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,

Recommended Answers

All 4 Replies

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.

And this way you can call it...

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

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

Welcome.. make this thread solved :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.