php category, sub category tree

前端 未结 3 1412
别跟我提以往
别跟我提以往 2021-01-23 10:29

I\'ve done this before in long complicated statements but I\'m trying to clean up some code.

I have the following table structures

parent_categories              


        
3条回答
  •  时光说笑
    2021-01-23 11:24

    $qry = $conn->prepare('
      SELECT   a.pid, a.parent_name, b.category_name
      FROM     parent_categories a
          JOIN child_categories  b ON a.pid = b.lpid
      ORDER BY a.pid
    ');
    
    if ($qry->execute()) {
      echo '
      '; $row = $qry->fetch(); while ($row) { $current_pid = $row['pid']; echo '
    • ', htmlentities($row['parent_name']), '
        '; do { echo '
      • ', htmlentities($row['category_name']), '
      • '; } while ($row = $qry->fetch() and $row['pid'] == $current_pid); echo '
    • '; } echo '
    '; }

提交回复
热议问题