0

I am a beginner to PHP, I have a table dyn_menu to store menu items in the database. The parent_id is the id of the parent menu item. I need to generate an array like,

array[

-1
--3
-2
--4
---6
-5

]

The table shown below.Please help me to solve this issue.

+----+-----------------+----------+-----------+
| id | label | link_url | parent_id |
+----+-----------------+----------+-----------+
| 1 | Home | # | 0 |
| 2 | About | # | 0 |
| 3 | Sub home | # | 1 |
| 4 | Sub contact | # | 2 |
| 5 | About Us | # | 0 |
| 6 | Sub sub contact | # | 4 |
+----+-----------------+----------+-----------+

Thanks,

Edited by anooptech: n/a

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by vibhaJ
0

I have made this function after mane efforts.
This will surely help you.
select is a function which will return two dimension array from query.

function catArray($id,$dash,$separate='__')
{
	$resCat = select("select * from category where parentId=".$id);
	for($i=0;$i<count($resCat);$i++)
	{
		$con='';
		for($j=1;$j<$dash;$j++)
			$con.=$separate;
		$arr[$resCat[$i]['categoryId']] = $con.$resCat[$i]['name'];
		
		$temp = select("select * from category where parentId=".$resCat[$i]['categoryId']);
		if(count($temp)>0)
		{	
			$dash++;
			$res = catArray($resCat[$i]['categoryId'],$dash,$separate);						
			$arr = $arr + $res;						
			$dash--;					
		}
	}
	return $arr;
}

Let me know output.

0

And this way you can call it...

$catAll = catArray(0,1,'&sdot;&sdot;&sdot;&sdot;&sdot;');
echo '<pre>';
print_r($catAll);
0

Thanks for the clue.. I solved using recursive function.. Thanks once again .. :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.