Display hierarchical data

后端 未结 2 1324
南旧
南旧 2021-01-15 19:05

I am playing around with a code example i found here about \'tree menu\' and wanted to make this question.

function tree($id)
{
$query = \"SELECT `name`,`id`         


        
2条回答
  •  被撕碎了的回忆
    2021-01-15 19:27

    I have a store with recursive inventory categories similar to what you're doing. Here's the code I wrote to present it as an unordered list (slightly modified for this example). Hope it helps!

      /**
       * Inventory Categories
       */
      function inventoryCategories() {
    
        $result = mysql_query("select * from store_inventorycategory");
    
        $rows = array();
    
        while ($row = mysql_fetch_array($result)) {
          $p = $row['parent_category_id'];
          $id = $row['id'];
          $rows[$id] = array('id'=>$id, 'parent'=>$p, 'title'=>$row['title'], 'children'=>array());
        }
    
        foreach ($rows as $k=>&$v) {
          if ($v['parent'] == $v['id']) continue;
          $rows[$v['parent']]['children'][] = &$v;
        }
    
        array_splice($rows,1);
    
        echo '
      '; recurseInventoryCategory($rows[0]); echo '
    '; } /** * display inventory category tree */ function recurseInventoryCategory ($o) { echo "
  • {$o['title']}
      "; foreach ($o['children'] as $v) { recurseInventoryCategory($v); } echo "
  • "; }

提交回复
热议问题