/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] help with database driven menu

i want to put my menu into a mysql database.
i have a table like this:
fb_menu (id, name, main_category, level, status)

you can see a static version here:
[url]http://www.morganwaage.com/fb/sub.php?menu=re&cat=re_main[/url]

and the database version here:
[url]http://www.morganwaage.com/fb/sub2.php?menu=re&cat=re_main[/url]

the difference is in the “Om forbrukerguiden” button. when you hover over it, it is suposed to give you two level2 buttons… but it is not working…
can anyone see in the code below what i am doing wrong???

[code=php]
$sql = “SELECT * FROM fb_menu WHERE status = ‘active’ AND main_category = ‘re’;”;
$result = mysql_query($sql, $conn);

echo “<ul class=’level1′>n”;

while ($row = mysql_fetch_array($result) ) {
$id = $row[‘id’];
$name = $row[‘name’];
$main_category = $row[‘main_category’];
$level = $row[‘level’];
$status = $row[‘status’];

if($level == 1 || $level == 0){
if($level == 1){
echo “<li><a href=’#’>” . $name . “</a></li>n”;
}
elseif($level == 0){
echo “<li class=’submenu’><a href=’#’>” . $name . “</a>n”;
echo “<ul class=’level2′>”;
echo “<li><a href=’#’>” . $name . “</a></li>n”;
echo “</ul>”;
echo “</li>”;
}
}
}[/code]

this is the static html that i am trying to generate:

[code=html]
<ul class=”level1″>
<li><a href=”#”>Kraf før reisen</a></li>
<li><a href=”#”>Krav etter reisen</a></li>
<li><a href=”#”>Reklamasjon</a></li>
<li><a href=””>Avbestilling</a></li>
<li class=”submenu”><a href=”#”>Om forbrukerguiden</a>
<ul class=”level2″>
<li><a href=”#”>Generelt</a></li>
<li><a href=”#”>Ta kontakt</a></li>
</ul>
</li>
<li><a href=”#”>Nyttige lenker</a></li>
</ul>
[/code]

to post a comment
PHP

20 Comments(s)

Copy linkTweet thisAlerts:
@javaforhireJan 05.2007 — You are echo'ing too much. You can store those li's in a variable, or, you can adjust your sql statement as follows, and you wont have to do as much logic juggling:

$sql = "SELECT * FROM fb_menu WHERE status = 'active' AND main_category = 're' ORDER BY 'level' ASC";

Now your levels are in order.
Copy linkTweet thisAlerts:
@demiurgenauthorJan 05.2007 — but if you look at the db driven menu you will see that it doesn't give me the level2 buttons when you hover over the "Om forbrukerguiden" button. that is the problem.

it's not the order that is wrong it's the whole menu i think... maybe the way it loops through the array or something...?

i guess what i am asking is:

does anyone know how i can generate the same html as the static menu, but from a db using php? ?
Copy linkTweet thisAlerts:
@NightShift58Jan 05.2007 — Are you sure that "om_kontakt" is in your table?
Copy linkTweet thisAlerts:
@demiurgenauthorJan 05.2007 — the "om_kontakt" button is in the table and the level is set to 2.

maybe i need to do an if statement for level2...?
Copy linkTweet thisAlerts:
@MrCoderJan 05.2007 — I would do this differently.

Replace "level" with "parent_id" and preform 2 MySQL queries for each parent menu.

For example,

TOP (id = 0) << This is an invisible top object

Kraf før reisen (id = 1, parent_id = 0)

Krav etter reisen (id = 2, parent_id = 0)

......

Om forbrukerguiden (id = 4, parent_id = 0)

-> Om forbrukerguiden (id = 5, parent_id = 4)

-> Ta kontakt (id = 6, parent_id = 4)

Nyttige lenker (id = 7, parent_id = 0)

Then MySQL statements, I would use 2 loops.

The first loop would cycle through all the menu items with a parent_id of "0"

The second loop would be called within the first loop to find all the menu items with a parent_id that matches the id of the menu item used within the first loop.

This is limited to 2 levels, but could be expanded to have more levels but creating a self calling function, but that is more complex.

Edit: typo "The" "Then"
Copy linkTweet thisAlerts:
@NightShift58Jan 05.2007 — I think you need to do a little bit more than that...

Can you list the contents of the DB table?

EDIT: Crossover... Sorry...
Copy linkTweet thisAlerts:
@NightShift58Jan 05.2007 — Let's see what the table contains. It may have all the necessary info - with other names.
Copy linkTweet thisAlerts:
@demiurgenauthorJan 06.2007 — sorry about the delay.... i fell asleep...

i have put two screenshots from phpmyadmin here:

http://www.morganwaage.com/fb/sub2.php?menu=re&cat=re_main
Copy linkTweet thisAlerts:
@NightShift58Jan 06.2007 — It could have worked with "level" instead of "parent_id" if the data hab been okay.

The problem was that id=5 ("Om forrukerguiden") was set to level=0 whereas it should have been level=1 like the others. The levels in id=6 and id=7 should have been set to level=5, what amounts to the parent ID.

But the name "parent_id" is a much better name for what is trying to be done here. Six months later, you'll still know exactly what it means.

Based on the pseudo-code/algorithm posted earlier by MrCoder, I came up with this:[code=php]<?php
$sql = " SELECT * FROM fb_menu ";
$sql .= " WHERE main_category = 're' ";
$sql .= " AND status = 'active' ";
$sql .= " ORDER BY parent_id, id ";
$result = mysql_query($sql);

$menu = array();
WHILE ($row = mysql_fetch_array($result) ) :
$menu[$row['parent_id']][$row['id']] = $row['name'];
ENDWHILE;

print "<ul class='level1'>n";
FOREACH ($menu[0] as $key => $thisITEM) :
IF (!isset($menu[$key])) :
print "<li><a href='#'>" . $thisITEM . "</a></li>n";
ELSE :
print "<li class='submenu'><a href='#'>" . $thisITEM . "</a>n";
print " <ul class='level2'>n";
FOREACH ($menu[$key] as $subkey => $subITEM) :
print " <li><a href='#'>" . $subITEM . "</a></li>n";
ENDFOREACH;
print " </ul>n";
print "</li>n";
ENDIF;
ENDFOREACH;
print "</ul>n";
?>[/code]
Please note that this will only work as a two-level menu system. If you ever need more, the loops will have to be farmed out to a recursive function.

MrCoder's solution will work, too. I just wanted to try to load the menu with a single SELECT and still generate the menu structure you wanted.
Copy linkTweet thisAlerts:
@demiurgenauthorJan 06.2007 — IT WORKS!

http://www.morganwaage.com/fb/sub3.php?menu=re&cat=re_main

?

however i do need more levels in two of the other categories on this project. but first i need to understand your code better...

THANX A MILLION!!!
Copy linkTweet thisAlerts:
@NightShift58Jan 06.2007 — You need more levels (as in depth) or you need to add submenus to other categories?
Copy linkTweet thisAlerts:
@demiurgenauthorJan 07.2007 — as in depth.... if you click on the "varer" buttons on the right side you can see it by hovering over "kontraktsbrudd" and then hover over "selgerens sanksjoner".
Copy linkTweet thisAlerts:
@NightShift58Jan 07.2007 — Is there a fixed maximum number of menu levels or do you want this "infinite"?
Copy linkTweet thisAlerts:
@demiurgenauthorJan 07.2007 — i now have 3 levels and to preserve userfriendlyness i think 3 should be the maximum.
Copy linkTweet thisAlerts:
@NightShift58Jan 07.2007 — There she goes... with 3 levels... quick and dirty...:[code=php]<?php
require "DBconnect.inc.php";

$sql = " SELECT * FROM fb_menu ";
$sql .= " WHERE main_category = 're' ";
$sql .= " AND status = 'active' ";
$sql .= " ORDER BY parent_id, id ";
$qry = mysql_query($sql);

$menu = array();
WHILE ($row = mysql_fetch_array($qry) ) :
$menu[$row['parent_id']][$row['id']] = $row['name'];
ENDWHILE;

print "<ul class='level1'>n";
FOREACH ($menu[0] as $LVL1_key => $LVL1_item) :
IF (!isset($menu[$LVL1_key])) :
print "<li><a href='#'>" . $LVL1_item . "</a></li>n";
ELSE :
print "<li class='submenu'><a href='#'>" . $LVL1_item . "</a>n";
print "<ul class='level2'>n";
FOREACH ($menu[$LVL1_key] as $LVL2_key => $LVL2_item) :
IF (!isset($menu[$LVL2_key])) :
print "<li><a href='#'>" . $LVL2_item . "</a></li>n";
ELSE :
print "<li class='submenu'><a href='#'>" . $LVL2_item . "</a>n";
print "<ul class='level3'>n";
FOREACH ($menu[$LVL2_key] as $LVL3_key => $LVL3_item) :
print "<li><a href='#'>" . $LVL3_item . "</a></li>n";
ENDFOREACH;
print "</ul>n";
print "</li>n";
ENDIF;
ENDFOREACH;
print "</ul>n";
print "</li>n";
ENDIF;
ENDFOREACH;
print "</ul>n";
?>
[/code]
Copy linkTweet thisAlerts:
@demiurgenauthorJan 07.2007 — thank you for that!!! you are a legend NightShift58!!

http://www.morganwaage.com/fb/sub.php?menu=va&cat=va_main
Copy linkTweet thisAlerts:
@NightShift58Jan 07.2007 — ... to which I can only reply: you apparently have not met too many legends, as I'm about as far from being one as one can get... but many thanks for the kind words...
Copy linkTweet thisAlerts:
@demiurgenauthorJan 08.2007 — do you know how i can get the categories of each button into the array?

i guess that is what i have to do to have the links work...?
Copy linkTweet thisAlerts:
@NightShift58Jan 08.2007 — Do you mean "main_category"? I'm not sure what you mean by that...
Copy linkTweet thisAlerts:
@demiurgenauthorJan 08.2007 — http://www.morganwaage.com/fb/sub.php?menu=va&cat=va_main

if you look at the link above you see the variable cat.

this is how i get the content, by including the cat in every button.

so i need it to also output the category field from the db table so i can put it into the url.
[code=php]
<li><a href='sub.php?menu=" . $cat_menu . "&cat=" . $categ . "'>" . $LVL1_item . "</a></li>n
[/code]

i have put the categ variable into the while loop, but it only outputs the last category in the db on all the url's...
[code=php]
$menu = array();
while ($row = mysql_fetch_array($qry) ) {
$menu[$row['parent_id']][$row['id']] = $row['name'];
$categ = $row['category'];
}
[/code]
×

Success!

Help @demiurgen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...