Php 从mysql获取主类别的子类别

Php 从mysql获取主类别的子类别,php,mysql,Php,Mysql,我在从mysql数据库检索子类别时遇到一些问题。我想显示父类别的子类别。我只能获取主类别的最后一个子类别。第一个子类别不显示**。在我的表格**中,我有category\u id和category\u parent\u id。其中category\u parent\u id对于父类别将为“0”。。提前感谢 <ul class="betterList"> <?php $con = mysql_connect("localhost","root","pwd")

我在从mysql数据库检索子类别时遇到一些问题。我想显示父类别的子类别。我只能获取主类别的最后一个子类别。第一个子类别不显示**。在我的表格**中,我有category\u id和category\u parent\u id。其中category\u parent\u id对于父类别将为“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 ")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['category_id'].$row['name_en-GB'];

                $result1=mysql_query("select * from table where category_parent_id=".$category_id)or die("No data available with this name"."<br/><br/>".mysql_error());
                 echo  $num_row    = mysql_num_rows($result1);

                    if($num_row>0) {
                        for($i=0;$i<$num_row;$i++)
                        {
                             while($row1=mysql_fetch_array($result1))
                             {

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

                ?>


                </li>

                <?php } ?>



    <?php }?> 
 </ul>

当我移除位于末尾的标签并将其保留在后面时,我可以显示所有子类别,但css不适用于此。那里出现了一些问题,但我无法解决它

请删除下面的内容,然后重试:

for($i=0;$i<$num_row;$i++)
{
for($i=0;$iWow!o_o
您正在使用旧的mysql_*函数

你写道: ($i=0;$i0)的
{

对于($i=0;$i),不要执行嵌套循环,而是使用联接获取所有内容:

SELECT t1.category_id parent, t1.`name_en-GB` parent_name,
       t2.category_id child, t2.`name_en-GB` child_name
FROM table t1
JOIN table t2 ON t2.parent_category_id = t1.category_id
WHERE t1.parent_category_id = 0
那么您的循环将是:

$last_parent = null;
$first_cat = true;
while ($row = mysql_fetch_assoc($result)) {
    if ($row['parent'] != $last_parent) {
        $last_parent = $row['parent'];
        if (!$first_cat) { // Close out the last UL and LI
            echo '</ul></li>';
        } else {
            $first_cat = false;
        }
        echo '<li>' . $row['parent'] . $row['parent_name'];
        echo '<ul style="margin:0px;padding:0;">';
    }
    echo '<li>' . $row['child_name'] . </li>
}
if (!$first_cat) {
    echo '</ul></li>';
}
$last\u parent=null;
$first_cat=true;
while($row=mysql\u fetch\u assoc($result)){
如果($row['parent']!=$last\u parent){
$last_parent=$row['parent'];
如果(!$first_cat){//结束最后一个UL和LI
回音“”;
        <?php 
          echo "<ul>";
          while($row1=mysql_fetch_array($result1))
             {
         ?>
               <li><?php echo $row1['name_en-GB']?></li>
                <?php
             }
                echo " </ul>";
}否则{ $first_cat=false; } 回显“
  • ”.$row['parent']。$row['parent_name'];
            <?php 
              echo "<ul>";
              while($row1=mysql_fetch_array($result1))
                 {
             ?>
                   <li><?php echo $row1['name_en-GB']?></li>
                    <?php
                 }
                    echo " </ul>";
    
    echo'
      <?php echo "<ul>"; while($row1=mysql_fetch_array($result1)) { ?> <li><?php echo $row1['name_en-GB']?></li> <?php } echo " </ul>";
  • } 回显“
  • ”.$row['child_name']
  •         <?php 
              echo "<ul>";
              while($row1=mysql_fetch_array($result1))
                 {
             ?>
                   <li><?php echo $row1['name_en-GB']?></li>
                    <?php
                 }
                    echo " </ul>";
    
    } 如果(!$first\u cat){ 回音“”;
            <?php 
              echo "<ul>";
              while($row1=mysql_fetch_array($result1))
                 {
             ?>
                   <li><?php echo $row1['name_en-GB']?></li>
                    <?php
                 }
                    echo " </ul>";
    
    }

    您的代码中嵌套的循环太多:您有一个
    for
    while
    循环,这两个循环都试图循环内部查询的行。此外,您将每个子项放入自己的
    ,这可能不是您想要的。

    只要尝试一下这个解决方案是否适用于u,如果它适用,请调整您的代码相应地编码

            $result = mysql_query("select * from table WHERE category_parent_id = 0 ");
    
            while($row=mysql_fetch_array($result)) {
    
                $parent_id = $row['category_parent_id'];
                $query = mysql_query("select * from table where category_parent_id = {$parent_id}");
    
                while($sub_cats=mysql_fetch_array($query)) {
                    echo '<pre>'.print_r($sub_cats).'</pre>';
                }
    
            } 
    
    $result=mysql\u query(“从表中选择*,其中category\u parent\u id=0”);
    while($row=mysql\u fetch\u数组($result)){
    $parent_id=$row['category_parent_id'];
    $query=mysql\u query(“从表中选择*,其中category\u parent\u id={$parent\u id}”);
    而($sub_cats=mysql_fetch_数组($query)){
    回显“”。打印($sub_cats)。“”;
    
            <?php 
              echo "<ul>";
              while($row1=mysql_fetch_array($result1))
                 {
             ?>
                   <li><?php echo $row1['name_en-GB']?></li>
                    <?php
                 }
                    echo " </ul>";
    
    } }
    只要在循环之前添加内部
    ,我就可以得到子类别

    
    

  • 即使在删除后,它也会给出相同的结果$num_row的结果是什么?它会给出主目录的子目录数。是的,我问的是行数countFirst,你从最小值开始,所以请在first while中添加break关键字。我指的是在浏览器echo$num_row=mysql_num_rows中显示的类别($result1);我删除了它,但它给出了相同的输出。我知道category_id和category_parent_id在同一个表中。当您将表与自身联接时,这通常称为自联接。