 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
12-07-2007, 08:26 AM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Location: Denmark
Posts: 21
Thanks: 4
|
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.
|
|
|
12-07-2007, 12:35 PM
|
#3 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Location: Denmark
Posts: 21
Thanks: 4
|
@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. 
|
|
|
12-07-2007, 11:17 AM
|
#4 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
Do you mean in the following format?
PHP Code:
- Backup - Tape - LTO - RDX - Cleaner
- Motherboard - Intel - AMD
|
|
|
|
12-07-2007, 11:33 AM
|
#5 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Location: Denmark
Posts: 21
Thanks: 4
|
Yes, in a nested list like so...
Code:
<ul>
<li>Backup
<ul>
<li>Tape
<ul>
<li>LTO</li>
<li>RTO</li>
...
</ul>
</li>
</ul>
</li>
<li>Motherboard
<ul>
...
</ul>
</li>
</ul>
I think I need to somehow split/explode the strings into an multidimensional array. From there I would output the list.
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.
|
|
|
12-07-2007, 02:19 PM
|
#6 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
12-07-2007, 04:32 PM
|
#7 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
That was a tough little problem. Here's a solution I came up with:
php Code:
<?phpclass ProductList { private $m_aList; public function __construct($aProducts) { $this-> m_aList = array(); foreach ($aProducts as $szProductTree) { $this-> parseProductTree($szProductTree); } } public function parseProductTree ($szProduct) { // First of all split the string into an array $aParts = split('/', $szProduct); // Get a reference to root array $aList = & $this-> m_aList; $iLastIndex = count($aParts) - 1; foreach ($aParts as $iIndex => $szLabel) { // This is the last node in the list, we need to handle this // differently because it will not contain any child nodes if ($iIndex == $iLastIndex) { if (key_exists($szLabel, $aList)) { break; } $aList[] = $szLabel; break; } // Check if node exists in the current array (specified by $aList), // if it doesn't exist, we add it to the array if (! key_exists($szLabel, $aList)) { $aList[ $szLabel] = array(); } // Update the array reference to point to the array we just added // The next node in the foreach loop will be added to this array $aList = & $aList[ $szLabel]; } } public function getList () { return $this-> m_aList; }}class ProductList_RenderHtml { public function render ($aList) { $szHtml .= "<ul>\n"; foreach ($aList as $mNodeName => $mNodeValue) { $szHtml .= $this-> renderList($mNodeName, $mNodeValue); } $szHtml .= "</ul>\n"; return $szHtml; } public function renderList ($szListLabel, $aList) { $szHtml .= "<li>" . $szListLabel . "\n"; $szHtml .= "<ul>\n"; foreach ($aList as $mNodeName => $mNodeValue) { // If the node value is an array we render it as a new list if (is_array($mNodeValue)) { $szHtml .= $this-> renderList($mNodeName, $mNodeValue); } // If its not an array we know it's list item else { $szHtml .= "<li>$mNodeValue</li>\n"; } } $szHtml .= "</ul>\n"; $szHtml .= "</li>\n"; return $szHtml; }}$aProducts = array( 'Backup/Tape/LTO', 'Backup/Tape/RDX', 'Backup/Tape/Cleaner', 'Motherboard/Intel', 'Motherboard/AMD'); $pProductList = new ProductList ($aProducts); $pRender = new ProductList_RenderHtml (); echo $pRender-> render($pProductList-> getList()); ?>
Hope it helps.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Last edited by Karl : 12-07-2007 at 08:50 PM.
Reason: Cleaned up code comments
|
|
|
|
|
The Following 4 Users Say Thank You to Karl For This Useful Post:
|
|
12-07-2007, 06:01 PM
|
#8 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Location: Argentina
Posts: 72
Thanks: 18
|
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:
// Get a reference to root node $aList = &$this->m_aList;
I never saw this before. Whats supposed to be/do?,
thank you
|
|
|
|
12-07-2007, 08:57 PM
|
#9 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
Quote:
Originally Posted by Matt83
I never saw this before. Whats supposed to be/do?
|
It just provides a reference (pointer) to the array. You can then add elements to the array via the reference.
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).
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
|
|
|
|
|
The Following 2 Users Say Thank You to Karl For This Useful Post:
|
|
12-07-2007, 09:03 PM
|
#10 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
In extension to Karl's, consider this:
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:
$aArray = array('Apple', 'Orange', 'Kiwi', 'Pear'); $pItem =& $aArray[ 2]; $pItem = 'Pitaya';
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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
|
The Following 5 Users Say Thank You to Wildhoney For This Useful Post:
|
|
12-08-2007, 01:13 AM
|
#11 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
Quote:
Originally Posted by Wildhoney
In extension to Karl's, consider this:
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:
$aArray = array('Apple', 'Orange', 'Kiwi', 'Pear'); $pItem =& $aArray[ 2]; $pItem = 'Pitaya';
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.
|
Thanks, got that. 
|
|
|
|
12-08-2007, 12:27 AM
|
#12 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Location: Argentina
Posts: 72
Thanks: 18
|
Perfect, got it. Thanks guys 
|
|
|
|
12-08-2007, 03:04 AM
|
#13 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Location: US
Posts: 66
Thanks: 19
|
Thats very cool Wildhoney, you are full of useful knowledge. 
|
|
|
12-08-2007, 05:01 PM
|
#14 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Location: Denmark
Posts: 21
Thanks: 4
|
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! 
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|