PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
$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
Last edited by lockma on Wed Apr 20, 2011 2:40 am, edited 1 time in total.
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.
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.
$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>';