note 86316 modified in function.array-walk-recursive by danbrown
| From: | danbrown@php.net | Date: | Mon, 13 Oct 2008 01:48:14 +0000 |
| Subject: | note 86316 modified in function.array-walk-recursive by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-145301@lists.php.net to get a copy of this message | ||
Nice system to make DB tree using function array_walk_recursive:
<?php
function _CategoriesTree(&$item, $key)
{
global $objDB;
if ($key=="childs")
{
$res = $objDB->getAssoc('SELECT category_id, name,category_parent_id, category_id AS
childs FROM categories WHERE category_parent_id = '.$item.' ORDER BY orderid');
if(!empty($res))
{
// walk inside subcategories
$item = $res;
}
else
{
// equvalent => no childs
$item = -1;
}
}
}
$categories = $objDB->getAssoc('SELECT category_id, name,category_parent_id, category_id AS
childs FROM categories WHERE category_parent_id = 0 ORDER BY orderid'); // fetchPairs ->
value is the index
array_walk_recursive($categories, 'treeFunc',$objDB);
// test
print_r($categories);
?>
--was--
Nice system to make DB tree using function array_walk_recursive:
function _CategoriesTree(&$item, $key)
{
global $objDB;
if ($key=="childs")
{
$res = $objDB->getAssoc('SELECT category_id, name,category_parent_id, category_id AS
childs FROM categories WHERE category_parent_id = '.$item.' ORDER BY orderid');
if(!empty($res))
{
// walk inside subcategories
$item = $res;
}
else
{
// equvalent => no childs
$item = -1;
}
}
}
$categories = $objDB->getAssoc('SELECT category_id, name,category_parent_id, category_id AS
childs FROM categories WHERE category_parent_id = 0 ORDER BY orderid'); // fetchPairs ->
value is the index
array_walk_recursive($categories, 'treeFunc',$objDB);
// test
print_r($categories);
http://php.net/manual/en/function.array-walk-recursive.php