如何使用php从mysql数据库创建无序列表

时间:2017-11-22 04:41:38

标签: php html

我做了一个显示无序列表的代码。但它没有正常工作我如何正确。我是新来的PHP请提前帮助我。

        <ul>
          <li class='mainnode'>A</li>
          <ul>
            <li class='chlnode'>A1</li>
            <ul>
              <li class='chlnode'>A3</li>
            </ul>
            <ul></ul>
            <li class='chlnode'>A2</li>
            <ul>
              <li class='chlnode'>A4</li>
            </ul>
            <ul>
              <ul>
                <li class='chlnode'>A5</li>
              </ul>
              <ul></ul>
            </ul>
          </ul>
          <li class='mainnode'>B</li>
          <ul>
            <li class='chlnode'>B1</li>
            <li class='chlnode'>B2</li>
            <ul>
              <li class='chlnode'>B3</li>
            </ul>
            <ul>
              <ul>
                <li class='chlnode'>B4</li>
              </ul>
              <ul></ul>
            </ul>
          </ul>
          <li class='mainnode'>C</li>
          <ul>
            <li class='chlnode'>C1</li>
            <ul>
              <li class='chlnode'>C2</li>
            </ul>
            <ul>
              <ul>
                <li class='chlnode'>C3</li>
              </ul>
              <ul>
                <ul>
                  <li class='chlnode'>C4</li>
                </ul>
                <ul>
                  <ul>
                    <li class='chlnode'>C5</li>
                  </ul>
                </ul>
              </ul>
            </ul>
          </ul>
          <li class='mainnode'>D</li>
          <ul>
            <li class='chlnode'>D1</li>
            <ul>
              <li class='chlnode'>D4</li>
            </ul>
            <ul></ul>
            <li class='chlnode'>D3</li>
            <ul>
              <li class='chlnode'>D5</li>
            </ul>
            <ul>
              <ul>
                <li class='chlnode'>D6</li>
              </ul>
              <ul>
                <ul>
                  <li class='chlnode'>D7</li>
                </ul>
                <ul></ul>
              </ul>
            </ul>
          </ul>
          <li class='mainnode'>E</li>
          <ul>
            <li class='chlnode'>E1</li>
          </ul>
          <li class='mainnode'>F</li>
          <ul>
            <li class='chlnode'>F1</li>
          </ul>
          <li class='mainnode'>G</li>
          <ul>
            <li class='chlnode'>J</li>
            <ul>
              <li class='chlnode'>J1</li>
            </ul>
            <ul></ul>
          </ul>
        </ul>

这是我用于创建HTML代码的php函数。

<?php
function menu()
{
    echo "<ul>";
    //this is sql query for parent element
    $list = mysql_query("SELECT * FROM `abc` WHERE col2='0' ");
    while ($row = mysql_fetch_array($list)) {
        echo "<li class='mainnode'>" . $row['col3'] . "</li>";

        echo "<ul>";
        //this is sql query for child element    
        $list1 = mysql_query("SELECT * FROM `abc` WHERE `col2`= " . $row['col1']);

        while ($row = mysql_fetch_array($list1)) {
            echo "<li class='chlnode'>" . $row['col3'] . "</li>";

            $list2 = mysql_query("SELECT * FROM `abc` WHERE `col2`= " . $row['col1']);
            while ($row = mysql_fetch_array($list2)) {
                echo "<ul>" . "<li class='chlnode'>" . $row['col3'] . "</li>" . "</ul>";

                echo "<ul>";
                $list3 = mysql_query("SELECT * FROM `abc` WHERE `col2`=" . $row['col1']);
                while ($row = mysql_fetch_array($list3)) {
                    echo "<ul>" . "<li class='chlnode'>" . $row['col3'] . "</li>" . "</ul>";

                    echo "<ul>";
                    $list4 = mysql_query("SELECT * FROM `abc` WHERE `col2`= " . $row['col1']);

                    while ($row = mysql_fetch_array($list4)) {

                        echo "<ul>" . "<li class='chlnode'>" . $row['col3'] . "</li>" . "</ul>";


                        echo "<ul>";
                        $list5 = mysql_query("SELECT * FROM `abc` WHERE `col2`= " . $row['col1']);

                        while ($row = mysql_fetch_array($list5)) {

                            echo "<ul>" . "<li class='chlnode'>" . $row['col3'] . "</li>" . "</ul>";
                        }
                        echo "</ul>";
                    }
                    echo "</ul>";
                }
                echo "</ul>";
            }
        }
        echo "</ul>";
    }
    echo "</ul>";
}
?>

3 个答案:

答案 0 :(得分:0)

请勿使用mysql_不推荐使用mysqli_。尝试在while循环之前创建无序列表<ul>,并尝试使用<li>循环每个行值。

这里是一瞥。

<?php $list1 = mysqli_query($con,"SELECT * FROM abc WHERE col2= ".$row['col1'] ); ?>
<ul>
<?php while($row = mysqli_fetch_array($list1)) { ?>
        <li><?php echo $row['col3']; ?>"</li>

    <?php  $list2  = mysqli_query($con,"SELECT * FROM `abc` WHERE `col2`= ".$row['col1'] );  ?>
      <ul>
      <?php while($row = mysqli_fetch_array($list2)) 
        { ?>
           <li><?php echo $row['col3']; ?>"</li>
  <?php } ?>
      </ul>
   <?php } ?>
</ul>

答案 1 :(得分:-2)

所有嵌套的ul应该在Li中开始,如下所示。

<ul>
    <Li> c
      <ul>
         <Li> c1 
           <ul>
             <Li> c2 </Li>
           </ul>
         </Li>
      </ul>
     </Li>
  </ul>

希望这能解决显示问题

答案 2 :(得分:-2)

首先你需要从mysql中获取返回行的计数。 然后把一个for循环迭代并打印list.inside for循环你可以把你的while循环打印出来并按照你的意愿打印列表标签。