|
Posted by Angelos on 07/13/05 17:03
UPDATE FOR THE displayMenu() function;
> class menu
> {
> var $parent_id=0;
> var $level=0;
> var $tree = array();
> var $nodesCount=0;
> var $firstItem=1;
> var $lastItem=0;
> var $maxLevel=0;
> var $firstLevelCnt=0;
> var $i=0;
> var $x = array();
>
> function menu()
> {
> $this->nodesCount = 0;
> $this->tree = array();
> $this->treecnt = array();
> $this->maxLevel = 0;
> $this->firstLevelCnt = array();
> $this->firstItem = array();
> $this->lastItem = array();
> }
>
> function scanTableForMenu()
> {
> $dbresult = mysql_query('SELECT * FROM product_categories
> WHERE product_categories_id != 0
> ');
> $this->tmpArray = array();
> while ($row = mysql_fetch_array($dbresult))
> {
> $this->tmpArray[$row['product_categories_id']]['parent_id'] =
> $row['product_categories_parent_id'];
> $this->tmpArray[$row['product_categories_id']]['name'] =
> $row['product_categories_name'];
> $this->tmpArray[$row['product_categories_id']]['body'] =
> $row['product_categories_body'];
> $this->tmpArray[$row['product_categories_id']]['img'] =
> $row['product_categories_img'];
> }
> $this->depth($this->tmpArray,0,0);
> $this->displayMenu($this->tree);
> }
> function depth($tmpArray,$parent_id,$level)
> {
> reset ($tmpArray);
> foreach($tmpArray as $key => $value)
> {
> if ($value['parent_id'] == $parent_id)
> {
> unset($tmpArray[$key]);
> unset($this->tmpArray[$key]);
> $cnt = count($this->tree) + 1;
> $this->tree[$key]['level'] = $level;
> $this->tree[$key]['parent'] = $value['parent_id'];
> $this->tree[$key]['name'] = $value['name'];
> $this->tree[$key]['body'] = $value['body'];
> $this->tree[$key]['img'] = $value['img'];
> unset($value);
> if($key !=$parent_id)
> $this->depth($this->tmpArray,$key,$level+1);
> }
> }
> }
function displayMenu($tree,$currentLevel = 0)
{
//echo "<pre>";
//print_r($this->tree);
//echo "</pre>";
reset($this->tree);
echo "<ul>";
while (list($key, $value) = each($this->tree))// for($ii = 1;$ii
<=$this->cntFirstlevel;$ii++)
{
if($this->tree[$key]['level'] == $currentLevel)
{
echo "<li>";
echo $this->tree[$key]['name'];
echo "</li>";
unset($this->tree[$key]);
}
elseif($this->tree[$key]['level'] != $currentLevel)
{
$this->displayMenu($this->tree,$currentLevel+1);
}
}
echo "</ul>";
}
> $menu1 = new menu();
> $menu1->scanTableForMenu();
As it is it Displays all the levels but for only the first Branch
a.. Category_I
a.. sub_cat_I
a.. sub_sub_cat_I
[Back to original message]
|