I have an array tree built by an recursive function. Now, I want all the items to contain an notices/warnings counter. Just a number showing how many 'notices' that item have.
Now to the problem. I want all the items to show the total number of notices from itself AND it's children. But the recursive function is building starting from top parents and working down. So it goes the wrong way to count notices.
Like this:
Item 1 (3)
- - - Item 1.1 (1)
- - - Item 1.2 (2)
- - - - - - Item 1.2.1 (1)
- - - - - - Item 1.2.2 (1)
Item 2 (1)
- - - Item 2.1 (0)
- - - Item 2.2 (1)
And here is my recursive function (simplified):
<?php
public function tree($item_id)
{
global $wpdb;
$q = $wpdb->get_results("SELECT * FROM items WHERE parent_item_id = '".$item_id."'", "ARRAY_A");
foreach ($q as $key => $r)
{
$return[$key] = $r;
$return[$key]['notices'] = 1;
$return[$key]['children'] = $this->tree($r['item_id']);
}
return $return;
}
?>