2016-10-31 9 views
0

PHPでカテゴリとサブカテゴリを表示するために再帰関数を使用しています。 MySQLデータベースのテーブルで は、3つの列があります。PHPで再帰中にサブカテゴリを表示し、リストにクラスを追加する

cat_id, parent_id, cat_name 

しかし、私はjQueryのを使用して、ドロップダウンメニューを作るためにクラスを追加するために、順不同リストの追跡を維持する必要があります。私が使用しています

<ul> 
    <li class = "dropdown"><a href = "#">Parent 1</a> 
     <ul class = "sub-menu"> 
      <li><a href = "#">Sub 1</a></li> 
      <li class = "dropdown"> 
       <a href = "#">Sub 2</a> 
       <ul class = "sub-menu"> 
        <li><a href = "#">Sub 2.1</a></li> 
        <li><a href = "#">Sub 2.2</a></li> 
        <li><a href = "#">Sub 2.3</a></li> 
       </ul> 
      </li> 
      <li><a href = "#">Sub 3</a></li> 
      <li><a href = "#">Sub 4</a></li> 
      <li><a href = "#">Sub 5</a></li> 
     </ul> 
    </li> 
    <li><a href = "#">Parent 2</a></li> 
    <li><a href = "#">Parent 3</a></li> 
    <li><a href = "#">Parent 4</a></li> 
</ul> 

再帰関数::だから、HTMLコードは次のようになります

$query = "select * from categories";$result = $conn->query($query); $cats = array();while($cat = $result->fetch_assoc()){$cats_ID[$cat['cat_id']][] = $cat;$cats[$cat['parent_id']][$cat['cat_id']] = $cat;} 

function build_tree($cats,$cat_id,$only_parent = false){ 
    if(is_array($cats) and isset($cats[$cat_id])){ 
     $tree = "<ul>\n"; 

     if($only_parent==false){ 
      foreach($cats[$cat_id] as $cat){ 
       $tree .= '<li><a href = "#">'.$cat['cat_name']; 
       $tree .= build_tree($cats,$cat['cat_id']); 
       $tree .= "</a></li>\n"; 
      } 
     } 

     $tree .= "</ul>";   
    } 
    else return null; 

    return $tree; 
} 

どのように私は、ULリストを追跡することができますか?

+0

あなたが求めている質問は信じられないほど広いです。あなたに手伝っていただくために、より詳細な情報が必要です。 –

+0

再帰関数では、 '$ depth'引数をデフォルトで' 1'に追加します。そして、それぞれの回帰において '$ depth + 1'を渡します。これは、再帰のあなたの "深さ"を追跡します。あなたが再び退治するたびに、それは増えます。次に、 '$ depth'が' 1'より大きい場合、あなたはサブメニューに入っていてクラスを追加することがわかります。 JSで必要な場合に備えて、その要素に属性を設定することもできます。それ以外の場合は、再帰コードを変更する必要がある場合は、再帰コードを共有する必要があります。 –

+0

@JonathanKuhnが質問を編集しました –

答えて

0

これは、上記のコメントに$depthパラメータを追加して提案したようにする必要があります。

$query = "select * from categories"; 
$result = $conn->query($query); 
$cats = array(); 
while($cat = $result->fetch_assoc()){ 
    $cats_ID[$cat['cat_id']][] = $cat; 
    $cats[$cat['parent_id']][$cat['cat_id']] = $cat; 
} 

//added a depth argument that defaults to 1 
function build_tree($cats,$cat_id,$only_parent = false, $depth=1){ 
    if(is_array($cats) and isset($cats[$cat_id])){ 
     //check if depth is > 1, output class, else, no class. 
     if($depth > 1){ 
      $tree = "<ul class='sub-menu'>\n"; 
     } else { 
      $tree = "<ul>\n"; 
     } 

     if($only_parent==false){ 
      foreach($cats[$cat_id] as $cat){ 
       //get the recursion first 
       //adjusted recursive call to include depth+1. 
       $tmp = build_tree($cats,$cat['cat_id'],$only_parent,$depth+1); 

       //if we are at the first depth and there is child content 
       if($depth == 1 && is_null($tmp)){ 
        //add the dropdown class 
        $tree .= '<li class="dropdown"><a href = "#">'.$cat['cat_name']; 
       } else { 
        //no dropdown class 
        $tree .= '<li><a href = "#">'.$cat['cat_name']; 
       } 
       //now append the recursion 
       $tree .= $tmp; 
       $tree .= "</a></li>\n"; 
      } 
     } 
     $tree .= "</ul>";   
    } 
    else return null; 
    return $tree; 
} 

新しく追加された引数にデフォルト値が設定されているため、関数をこの新しい関数に置き換えるだけで済みます。

+0

完璧に動作します!どうもありがとうございました! –

+0

もう1つ質問...リスト要素にクラスを追加するにはどうすればよいですか? –

+0

のHTMLコードで、「depth == 1」のドロップダウンクラスを追加するように更新されました。 –

関連する問題