简体   繁体   中英

how do i reference a dynamically created list php

I have the following code to create category and subcategories from a database

function displayChild($parent)
{
    include 'connect.php';
    $parentid=$parent;
    $catSql='select* from categories where parentid='.$parentid.'';
    $result=$con->query($catSql);
    echo '<div><ul class='.'ul'.$parentid.'>';
    while ($rows=$result->fetch_assoc())
    {
        echo '<li><a href="#">'.mb_strtoupper($rows['catName']).'</a></li>';
        displayChild($rows['catId']);
    }
    echo '</ul></div>';
}
displayChild('0');

The CSS bits should be as follows

@charset "utf-8";
.ul0{}
.ul1{}
.ul2{}
.
.
.
.uln{}

since the first tag appears outside the while loop it forms a really weird list when i reference it.putting it inside the while() loop is not an option either. please help

I'm not exactly sure what you mean by " it forms a really weird list when i reference it." However, it sounds like maybe you are getting odd html. If that's the case, you get better results if you put the nested function call inside the li:

function displayChild($parent){
  include 'connect.php';
  $parentid=$parent;
  $catSql='select* from categories where parentid='.$parentid.'';
  $result=$con->query($catSql);
  echo '<div><ul class='.'ul'.$parentid.'>';

  while ($rows=$result->fetch_assoc()){
    echo '<li><a href="#">'.mb_strtoupper($rows['catName']).'</a>';
    displayChild($rows['catId']); // note this is now inside the list item
    echo '</li>';
  }
  echo '</ul></div>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM