简体   繁体   中英

php display multilevel treenode menu recursive

I have a post regarding this which was answered though I now have a different problem related to the same topic. You may want to reference here for the original question. php display multilevel treenode menu

Now that the array is displaying by tree-node, it seems that it is not fully recursive. I notice that when the first element is a child node, it is not displaying as a child.

I tried doing a krsort then ksort.

function getChildren(&$rows, $p = 0) {
    $r = array();
    krsort($rows);
    foreach($rows as $row_id => $row) {
        if ($row['parent_node']==$p) {
            $r[$row['product_category_code']] = getChildren($rows, $row['product_category_code']);
            unset($rows[$row_id]);
        }
    }
    ksort($rows);
    return $r;
}

Here's the structure of the array:

array
  0 => 
    array
      'product_category_code' => string 'akamia' (length=6)
      'product_category_desc' => string 'Akamia' (length=6)
      'parent_node' => string 'summer-dress' (length=12)
      'inactive' => string '0' (length=1)
      'sort' => string '0' (length=1)
      'created_by' => string '1' (length=1)
      'created_date' => string '2014-04-01 10:03:42' (length=19)
      'modified_by' => string '1' (length=1)
      'modified_date' => string '2014-04-01 10:03:47' (length=19)
  1 => 
    array
      'product_category_code' => string 'bracelets' (length=9)
      'product_category_desc' => string 'Bracelets' (length=9)
      'parent_node' => string '' (length=0)
      'inactive' => string '0' (length=1)
      'sort' => string '0' (length=1)
      'created_by' => string '1' (length=1)
      'created_date' => string '2014-03-14 22:04:08' (length=19)
      'modified_by' => string '1' (length=1)
      'modified_date' => string '2014-03-14 22:09:05' (length=19)
  2 => 
    array
      'product_category_code' => string 'floral-dress' (length=12)
      'product_category_desc' => string 'Floral Dress' (length=12)
      'parent_node' => string '' (length=0)
      'inactive' => string '0' (length=1)
      'sort' => string '0' (length=1)
      'created_by' => string '1' (length=1)
      'created_date' => string '2014-03-14 22:09:49' (length=19)
      'modified_by' => string '1' (length=1)
      'modified_date' => string '2014-04-01 10:03:30' (length=19)
  3 => 
    array
      'product_category_code' => string 'flowery-bracelets' (length=17)
      'product_category_desc' => string 'Flowery Bracelets' (length=17)
      'parent_node' => string 'bracelets' (length=9)
      'inactive' => string '0' (length=1)
      'sort' => string '0' (length=1)
      'created_by' => string '1' (length=1)
      'created_date' => string '2014-03-14 22:09:16' (length=19)
      'modified_by' => string '1' (length=1)
      'modified_date' => string '2014-03-30 19:08:44' (length=19)
  4 => 
    array
      'product_category_code' => string 'small-flowery-bracelets' (length=23)
      'product_category_desc' => string 'Small Flowery Bracelets' (length=23)
      'parent_node' => string 'flowery-bracelets' (length=17)
      'inactive' => string '0' (length=1)
      'sort' => string '0' (length=1)
      'created_by' => string '1' (length=1)
      'created_date' => string '2014-03-14 22:08:35' (length=19)
      'modified_by' => string '1' (length=1)
      'modified_date' => string '2014-03-30 19:09:44' (length=19)
  5 => 
    array
      'product_category_code' => string 'summer-dress' (length=12)
      'product_category_desc' => string 'Summer Dress' (length=12)
      'parent_node' => string '' (length=0)
      'inactive' => string '0' (length=1)
      'sort' => string '0' (length=1)
      'created_by' => string '1' (length=1)
      'created_date' => string '2014-03-14 22:09:29' (length=19)
      'modified_by' => string '0' (length=1)
      'modified_date' => null

The output (without the krsort / ksort) is showing this, which is wrong:

  • Akamai
  • Bracelets
    • Flowery Bracelets
      • Small Flowery Bracelets
  • Floral Dress
  • Summer Dress

This should be the output:

  • Bracelets
    • Flowery Bracelets
      • Small Flowery Bracelets
  • Floral Dress
  • Summer Dress
    • Akamai

Never mind, I got the answer. The $p on function getChildren(&$rows, $p = 0) should be $p = '' instead of a zero (0) since the parent node on the array is empty. Silly me. XD

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