![]() |
Making a navigation list from database
Hi peeps, I hope someone can help me out or point me to a tutorial or code which could help.
The challenge I want to make an unordered list (and sublists) from product groups in a database. I think I would need to use some kind of recursive function/looping through this. But I just can't wrap my mind around it. I've searched the Internet quite a bit and found a few directory->list scripts but nothing that I've been able to apply to my needs. I fetch product groups from database which are returned each in a string like so: Backup/Tape/LTO Backup/Tape/RDX Backup/Tape/Cleaner Motherboard/Intel Motherboard/AMD General solution: I need to split the string by / and check if the group exists already in an array, and if so, then if the subgroup does not exist, then create it. If the subgroup exists, then check the next element etc. But for me, this gets complex fast. In the meantime... I'm going to read some articles I found on recursion and traversing arrays etc. :-) Thank you everybody. |
Check out this tutorial: http://www.talkphp.com/showthread.php?p=5123
Hope that helps! :-) And welcome to the site! Why don't you take a minute and introduce yourself over here: http://www.talkphp.com/forumdisplay.php?f=14 |
Do you mean in the following format?
PHP Code:
|
Yes, in a nested list like so...
Code:
<ul>This is actually very close to working for me: Convert anything to Tree Structures in PHP But I'm battling the plotTree function to output it for me. |
@Tanax: I've taken a look and I don't think that's what I need. But thanks for your help.
I'll probably introduce myself today or tomorrow. :-D |
A warm welcome, Victorius! I did have a stab at this, but it would appear I'm not in such a frame a mind to be able to successfully do it yet! I'll have another go later if you're still having troubles.
|
That was a tough little problem. Here's a solution I came up with:
php Code:
Hope it helps. |
amazing stuff Karl, very simple solution. i gave this problem a try and all i ended up with was a headache and a code double in length than yours :-$ :-)
If you dont mind, i would like to ask you about this bit of the code: PHP Code:
thank you |
Quote:
I could have just added the elements to the $this->m_aList array, but because we were recursively traversing the array, and we wanted to add nodes to the last parent from the current level, it was easier to use the array reference to keep track of the parent array (node). |
In extension to Karl's, consider this:
php Code:
In the above example, both $a and $b now equal 2 because $b has been set as a reference to $a and so whatever $b becomes, $a becomes as well.In a more complicated example: php Code:
As $pItem is now a reference to the third item in our array, changing its value will also change the value in the array, so Kiwi now becomes Pitaya. |
Perfect, got it. Thanks guys ^^
|
Quote:
|
Thats very cool Wildhoney, you are full of useful knowledge. :-D
|
Wow
Wow, thank you all for your input. This is fantastic. I too just got a headache when trying to figure this out.
Thank you Karl, I'm going to see if I can put this to good use. Love this community. Thanks again everybody! ^^ |
| All times are GMT. The time now is 08:13 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0