Page 1 of 1

Help creating unordered list from recordset

Posted: Tue Apr 19, 2011 5:37 am
by lockma
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.

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>';
Please can someone help?

Many thanks

Mark

Re: Help creating unordered list from recordset

Posted: Wed Apr 20, 2011 1:52 am
by Christopher
You just need to add a "if ($row['Type'] != $prev_type) {" check and open/close another list.

It is very difficult to see what your code is doing because of the formatting. It would be easier for people to help if you cleaned up the indenting on your post.

Re: Help creating unordered list from recordset

Posted: Wed Apr 20, 2011 2:41 am
by lockma
Thanks Christopher, Thanks for the reply - i still cant get my head around the coding on this one - I have tidied up the code as per your suggestion though.

Re: Help creating unordered list from recordset

Posted: Wed Apr 20, 2011 1:55 pm
by Christopher
Maybe something like this -- hopefully you get the idea. This code is untested so may not run or work correctly.

Code: Select all

$prev_brand = '';
$prev_type = '';
echo '<ul>';
while ($row = mssql_fetch_array($dbr)) {

if ($row['Brand'] != $previous) {
// if not the first time, close the nested list
  if ($prev_brand != '') {
      echo '</ul></li>';
  }
  // display the category
  echo '<li>' . $row['Brand'];
  // open the nested list
  echo '<ul>';
  // store the current value for comparison next time
  $prev_brand = $row['Brand'];
  $prev_type = '';        // reset for each new Brand
}

if ($row['Type'] != $prev_type) {
// if not the first time, close the nested list
  if ($prev_type != '') {
      echo '</ul></li>';
  }
  // display the category
  echo '<li>' . $row['Type'];
  // open the nested list
  echo '<ul>';
  // store the current value for comparison next time
  $prev_type = $row['Type'];
}
// display the subcategories
echo '<li>' . $row['Type'] .'</li>';
// it's no longer the first time
$first = false;
}
echo '</ul></li></ul>';