Hi
I'm trying to turn some db data into a nested list structure to use as menus
first off, i fgured that it would be quicker to make one sql query, put the results in an array and then loop through that rather than making millions of queries - is this assumption correct ?
basically i have all my product families in a table - each family has a parent id (p_id) which is the fam_id of the family it belongs to, and its own family id (fam_id) and name (fam_nom)
the query i used is this
$famStructure = array();
$i=0;
$query = "SELECT * FROM `familles` ORDER BY p_id ASC";
$res = mysql_query($query);
while($row = mysql_fetch_array($res)){
$famStructure[$i] = array($row['p_id'],$row['fam_id'],stripslashes($row['fam_nom']));
//echo "<br />".$famStructure[$i][0]." - ".$famStructure[$i][1]." - ".$famStructure[$i][2];
$i++;
}
the resulting array looks like this when i echo it out
0 - 1 - fruits
0 - 2 - legumes
0 - 3 - volailles
0 - 4 - fromage
3 - 14 - Dindes
3 - 18 - Pintades
3 - 19 - Poulets
5 - 12 - Bresse
5 - 13 - Fermier
6 - 8 - noir
6 - 9 - marron
6 - 11 - rouge
14 - 15 - noir
14 - 16 - marron
14 - 17 - rouge
19 - 20 - Bresses
19 - 21 - Fermier
the first number is the parent id and the second is the id of the family
- you can see that the family "Fermier" is in the family "Poulets" which is in the family "volailles" which is at the root of the system (0)
now what i really can't work out is how to go about looping through that array to set up a list structure like this
<ul>
<li>fruits</li>
<li>legumes</li>
<li>volailles
<ul>
<li>Dindes</li>
<li>Pintades</li>
<li>Poulets
<ul>
<li>Bresses</li>
<li>Fermier</li>
</ul>
</li>
</ul>
</li>
<li>fromage</li>
</ul>
what's the best way of going about this ?
or maybe i should do it differently at the sql query level ?
has anyone go any suggestions ?
thanks