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 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.
<?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>
<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>
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.
Do yourself a favor: remove the MySQL credentials from your post.
Change your function to print the <ul></ul> just before and after the recursive call, not at the beginning of the function itself.
Then print the tags you want manually as you call the function: ie, like you do with the recursion, print the opening tag, call the function, and print the ending tag.
<?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 $parent === 0 ? '<ul class="menu">' : '<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)
?>
To be honost I am not sure how that code snippet works, but it does
I have just found your post as I had just discovered a problem with my script when going 3 levels deep.
Your script works a treat and so wanted to acknowledge and thank you for posting the working script on this forum.
The actual code is far superiorto my old code, it works (gee that's a plus - lol) and it has saved me a lot of time banging my head trying to get my previous clunky code working.