2013-10-05 62 views
0

我想創建一個包含類別和懸停類別我需要顯示子類別的列表。我能夠顯示列表中的父類別,但無法理解如何爲此獲取子類別。在我的表我有category_id,parent_id列和其他一些列。如果parent_id是'0',它是主類別,子類別包含category_id。因此,現在我需要顯示主類別的子類別。我無法理解如何繼續。可以給任何人提供建議。從數據庫中獲取子類別數據到列表

<ul class="betterList"> 
    <?php 
     $con = mysql_connect("localhost","root","pwd") or die('couldnot connect to database'.mysql_error()); 
     mysql_select_db("DB",$con); 
$result=mysql_query("select * from table order by `name_en-GB`")or die("No table available with this name"."<br/><br/>".mysql_error()); 
      while($row=mysql_fetch_array($result)) 
      { 
       $parent_id=$row['category_parent_id']; 
       $category_id=$row['category_id']; 
       if($parent_id==0) 

       { 
    ?> 
       <li><?php echo $row['name_en-GB'];?></li> 
       <?php } 
        ?> 


       <ul id="internal" style=" margin:0px; 
padding:0;"><li><?php //echo $row['name_en-GB']; ?></li><li>data</li></ul></li> 

    <?php 

      }?> 
</ul> 
+0

見http://stackoverflow.com/questions/17147229/php-recursive-directory-menu,http://stackoverflow.com/questions/10782810/echo-menu-tree-with -recursive-function和http://www.copterlabs.com/blog/build-menu-with-recursive-functions/ – Peter

回答

0

使用此代碼

<ul class="betterList"> 
    <?php 
    $con = mysql_connect("localhost","root","pwd") or die('couldnot connect to database'.mysql_error()); 
    mysql_select_db("DB",$con); 
    $result=mysql_query("select * from table where parent_id=0 order by `name_en-GB`")or die("No table available with this name"."<br/><br/>".mysql_error()); 
    while($row=mysql_fetch_array($result)) 
    { 
    $category_id=$row['category_id']; 
       ?> 
       <li><?php echo $row['name_en-GB']; ?></li> 

       <?php 
       $result1=mysql_query("select * from table where category_id=".$category_id." order by `name_en-GB`")or die("No table available with this name"."<br/><br/>".mysql_error()); 
       $num_row = mysql_num_rows($result1); 
       if($num_row>0) { 
        ?> 
        <ul id="internal" style=" margin:0px; padding:0;"> 
        <?php     
       } 
       while($row1=mysql_fetch_array($result1)) 
       {?> 
        <li><?php echo $row1['name_en-GB']; ?></li> 
       <?php 
       } 
       if($num_row>0) { 
        ?> 
        </ul> 
        <?php     
       } 
    } 
        ?> 
+0

感謝您的回覆,但它在子類別中顯示父類別名稱。 – user2083041

0

你錯過了一些代碼使用好的編輯。檢查我的註釋行

<ul class="betterList"> 
    <?php 
     $con = mysql_connect("localhost","root","pwd") or die('couldnot connect to database'.mysql_error()); 
     mysql_select_db("DB",$con); 
$result=mysql_query("select * from table order by `name_en-GB`")or die("No table available with this name"."<br/><br/>".mysql_error()); 
      while($row=mysql_fetch_array($result)) 
      { 
       $parent_id=$row['category_parent_id']; 
       $category_id=$row['category_id']; 
       if($parent_id==0) 
       { 
       ?> 
       <li><?php echo $row['name_en-GB'];?> // you miss this close tag 

       </li> 
       <?php // you miss this Open tag 
       } 
        ?> 


       <ul id="internal" style=" margin:0px; 
padding:0;"><li><?php //echo $row['name_en-GB']; ?></li><li>data</li></ul></li> 

    <?php 

      }?> 
</ul> 
相關問題