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>
4

2 回答 2

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>
于 2013-10-05T06:50:51.647 回答
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                   
                }
  }
                    ?>
于 2013-10-05T06:46:31.123 回答