Help creating unordered list from recordset
Posted: Tue Apr 19, 2011 5:37 am
Hi there,
I am trying to create an unordered list from a SQL recordset. My recordset is in the style of :-
Brand1, Type1, Model1
Brand1, Type1, Model2
Brand1, Type1, Model3
Brand1, Type2, Model4
Brand1, Type2, Model5
Brand2, Type3, Model6
Brand2, Type4, Model7
Brand2, Type4, Model8
etc etc N.B.
I do not know how many models are in each type or for each brand.
I am trying to get php to generate the following for me
[text]
<ul>
<li>Brand1
<ul>
<li>Type1
<ul>
<li>Model1</li>
<li>Model2</li>
<li>Model3</li>
</ul>
</li>
<li>Type2
<ul>
<li>Model4</li>
<li>Model5</li>
</ul>
</li>
</ul>
</li>
<li>Brand2
<ul>
<li>Type3
<ul>
<li>Model6</li>
</ul>
</li>
<li>Type4
<ul>
<li>Model7</li>
<li>Model8</li>
</ul>
</li>
</ul>
</li>
</ul>
[/text]
I "borrowed" the following code which gives me two levels but i need all three and i just cannot get my head around where to put what to include the model and only return what i want. I have found many examples online for single or two level lists but none for three.
Please can someone help?
Many thanks
Mark
I am trying to create an unordered list from a SQL recordset. My recordset is in the style of :-
Brand1, Type1, Model1
Brand1, Type1, Model2
Brand1, Type1, Model3
Brand1, Type2, Model4
Brand1, Type2, Model5
Brand2, Type3, Model6
Brand2, Type4, Model7
Brand2, Type4, Model8
etc etc N.B.
I do not know how many models are in each type or for each brand.
I am trying to get php to generate the following for me
[text]
<ul>
<li>Brand1
<ul>
<li>Type1
<ul>
<li>Model1</li>
<li>Model2</li>
<li>Model3</li>
</ul>
</li>
<li>Type2
<ul>
<li>Model4</li>
<li>Model5</li>
</ul>
</li>
</ul>
</li>
<li>Brand2
<ul>
<li>Type3
<ul>
<li>Model6</li>
</ul>
</li>
<li>Type4
<ul>
<li>Model7</li>
<li>Model8</li>
</ul>
</li>
</ul>
</li>
</ul>
[/text]
I "borrowed" the following code which gives me two levels but i need all three and i just cannot get my head around where to put what to include the model and only return what i want. I have found many examples online for single or two level lists but none for three.
Code: Select all
$previous = '';
$first = true;
echo '<ul>';
while ($row = mssql_fetch_array($dbr)) {
if ($row['Brand'] != $previous) {
// if not the first time, close the nested list
if (!$first) {
echo '</ul></li>';
}
// display the category
echo '<li>' . $row['Brand'];
// open the nested list
echo '<ul>';
// store the current value for comparison next time
$previous = $row['Brand'];
}
// display the subcategories
echo '<li>' . $row['Type'] .'</li>';
// it's no longer the first time
$first = false;
}
echo '</ul></li></ul>';
Many thanks
Mark