Re: Dynamic Multi Level Drop Down Menu in PHP/MYSQL

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At a glance it looks like you have everything you need in the  
database. Which is good -- I was expecting an issue there.

In this case, I'd probably do a nested loop -- you need to be able to  
loop through the submenu items. That also means limiting results at  
the different levels of loop to the current level ONLY.

Probably start with something like this:
$sql = 'SELECT * FROM menu WHERE parentid = PARENT_ID_GOES_HERE';

That way, when you loop through your results, all you're getting are  
the top level results for the child you're on. As you hit each  
result, you'll need to pop the database to check for children.

Personally, I'd move away from mysql_fetch_array -- it can be tough  
to manage, and gives you a lump set of info. My preference in a case  
like this would generally be a combination of a FOR loop and  
mysql_result.

Something like this seems appropriate:
for ($i = 0; $i < mysql_num_rows($rows); $i++)

Then, for each id that you roll across, you call your nested loop  
function with the SQL statement.

This will also give you the flexibility for if you have a lot of  
navigation, and a lot of children <ul>'s.

If you have any problems with the new code that you write, hit us up.
--
Joe

On Jun 1, 2010, at 11:08 AM, Sovichea SOU wrote:

> $sql = "SELECT * FROM menu";
> $rows = mysql_query($sql, $con);
>
> if(mysql_num_rows($rows > 0)){
>
> while($record = mysql_fetch_array($rows)){
> $title = $record["title"];
> echo "<li>$title</li>";
>
>
> }
>
>
> }
>
> On Tue, Jun 1, 2010 at 10:57 PM, Joe Forsythe
> <jforsythe@xxxxxxxxxxxxxxxxxx>wrote:
>
>>
>>
>> What is the PHP code that you're using right now?
>> --
>> Joe
>>
>>
>> On Jun 1, 2010, at 10:41 AM, Sovichea SOU wrote:
>>
>>> Hello Joe,
>>>
>>> Thanks for your fast reply.
>>>
>>> I can loop only like this:
>>>
>>> <ul>
>>> <li>Item1</li>
>>> <li>Item2</li>
>>> <li>Item2a</li>
>>> <li>Item2b</li>
>>> </ul>
>>>
>>> It should be look like this with nested list:
>>>
>>> <ul>
>>> <li>Item 1</li>
>>> <li>Item 2
>>> <ul>
>>> <li>Item 2a</li>
>>> <li>Item 2b</li>
>>> </ul>
>>> </li>
>>> </ul>
>>>
>>> This is database:
>>>
>>> id - title - link - parentid
>>> 1 - Item1 - http://www.yahoo.com - 0
>>> 2 - Item2 - http://www.google.com - 0
>>> 3 - Item2a - http://www.chhit.com - 2
>>> 4 - Item2b - http://www.gmail.com - 2
>>>
>>> Could you tell me how to write this kind of loop?
>>>
>>> Thanks,
>>>
>>> Vichea


[Non-text portions of this message have been removed]


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux