Unordered list script help
Posted: Sun Mar 22, 2009 7:16 pm
I have the unordered list script that I almost have working except on thing. What the script runs it puts an unwanted <ul> tag. If you run the script you will see that the first thing it does is output a <ul>. I want the script to know that it is being ran for the first time and NOT to output a <ul> but after that first time it is okay.
Actual output:
You can see here that the first line is a <ul>. I hard code that first <ul> as <ul class"menu"> so that is why I do not want the script to do that on the first run. Here is the desired.
Desired output:
Any help would greatly be appreciated.
Code: Select all
<?php
//connect to database
$link = mysqli_connect('','','');
mysqli_select_db($link,'');
//(1)get all rows
$query = mysqli_query($link,'SELECT * FROM products');
while ($row = mysqli_fetch_assoc($query)) {
$menu_array[$row['id']] = array('name' => $row['name'],'parent' => $row['parent_id']);
}
//(2)recursive function that prints categories as a nested html unorderd list
function generate_menu($parent) {
$has_childs = false;//this prevents printing 'ul' if we don't have subcategories for this category
global $menu_array;//use global array variable instead of a local variable to lower stack memory requirement
foreach($menu_array as $key => $value) {
if ($value['parent'] == $parent) {//if this is the first child print '<ul>'
if ($has_childs === false) {//don't print '<ul>' multiple times
$has_childs = true;
echo '<ul>';
}
echo '<li><a href="/category/' . $value['name'] . '/">' . $value['name'] . '</a>';
generate_menu($key);//call function again to generate nested list for subcategories belonging to this category
echo '</li>';
}
}
if ($has_childs === true) echo '</ul>';
}
//(3)generate menu starting with parent categories (that have a 0 parent)
?>
<html>
<head>
<title>TEST</title>
<link href="menu.css" rel="stylesheet" type="text/css">
<!--[if ie]>
<style type="text/css" media="screen">
.menu a {zoom:1;}
</style>
<![endif]-->
<script type="text/javascript" src="togglemenu.js"></script>
</head>
<body>
<div id="wrap">
<ul class="menu">
<?php
generate_menu(0);
?>
<script>menu_initiate();</script>
</div>
</body>
</html>Code: Select all
<ul>
<li><a href="/category/Desk/">Desk</a>
<ul>
<li><a href="/category/Oak Desk/">Oak Desk</a></li>
</ul>
</li>
<li><a href="/category/Chair/">Chair</a>
<ul>
<li><a href="/category/<span style='color:red;text-decoration:blink' title='Alert a moderator!'>grilled spam</span>/"><span style='color:red;text-decoration:blink' title='Alert a moderator!'>grilled spam</span></a></li>
<li><a href="/category/High Back Chair/">High Back Chair</a></li>
</ul>
</li>
<li><a href="/category/Bookshelf/">Bookshelf</a></li>
</ul>Desired output:
Code: Select all
<li><a href="/category/Desk/">Desk</a>
<ul>
<li><a href="/category/Oak Desk/">Oak Desk</a></li>
</ul>
</li>
<li><a href="/category/Chair/">Chair</a>
<ul>
<li><a href="/category/<span style='color:red;text-decoration:blink' title='Alert a moderator!'>grilled spam</span>/"><span style='color:red;text-decoration:blink' title='Alert a moderator!'>grilled spam</span></a></li>
<li><a href="/category/High Back Chair/">High Back Chair</a></li>
</ul>
</li>
<li><a href="/category/Bookshelf/">Bookshelf</a></li>
</ul>