从数据库到列表获取子类别数据

时间:2013-10-05 06:37:24

标签: php mysql

我想创建一个包含类别的列表,并悬停我需要显示子类别的类别。我能够在列表中显示父类别。但是无法理解如何获取子类别。在我的表中,我有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>

2 个答案:

答案 0 :(得分: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                   
                }
  }
                    ?>

答案 1 :(得分: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>