/    Sign up×
Community /Pin to ProfileBookmark

SQL based menu with submenues

Hi i can’t navigate the way i want to in my submenus. The loop goes to far. And i have some problem showing where you are in the menu.
Can someone please guide me in the right direction.
SQL table looks like this:
ID NAME parent_category
1 some_name 0
2 some_name2 0
3 sub_category 1
4 sub_sub_category 3
php code :

[code=php]function displayChildren($parent,$orginal = “”)
{
$result = mysql_query(“SELECT * FROM `kategorier` WHERE `parent_category` = $parent”);
echo “<ul>”;
while($item = mysql_fetch_object($result))
{
// echo “parent = $parent id = $item->id”;
if ($orginal == $item->id) {
echo “<li> – <a href=getcat.php?id=”.$item->id.”><b>” . $item->namn . “</b></a> id: “.$item->id.”</li>”;
} else {
echo “<li> – <a href=getcat.php?id=”.$item->id.”>” . $item->namn . “</a> id: “.$item->id.”</li>”;
}
// echo “orignal = $orginal. id = $item->id . Parent = $parent”;
if ($orginal == $item->id && $item->id != $parent) {
displayChildren($item->id,$orginal);
}
}
echo “</ul>”;
}
function getmainid($parent)
{
$sql = mysql_query(“SELECT * FROM `kategorier` WHERE `id` = $parent”) or die (mysql_error());
while($sqlrow=mysql_fetch_object($sql)){
$nyttid = $sqlrow->parent_category;
if ($sqlrow->type == “huvud”) {
$id = $sqlrow->id;
} else {
$id = getmainid($nyttid);
}
}
return $id;
}
// Skriver ut själva menyn
$id = $_GET[“id”];
$menu = mysql_query(“SELECT * FROM `kategorier` WHERE `parent_category` = 0”) or die (mysql_error());
echo “<ul>”;
while($item = mysql_fetch_object($menu))
{
$currentid = getmainid($id);
if ($currentid == $item->id) {
echo “<li><a href=getcat.php?id=”.$item->id.”><b>” . $item->namn . “</b></a> id: “.$item->id.”</li>”;
}else {
echo “<li><a href=getcat.php?id=”.$item->id.”>” . $item->namn . “</a> id: “.$item->id.”</li>”;
}
if ($item->id != $currentid)
continue;
$**** = displayChildren($item->id,$id);
print_r ($****);
}
echo “</ul>”;function displayChildren($parent,$orginal = “”)
{
$result = mysql_query(“SELECT * FROM `kategorier` WHERE `parent_category` = $parent”);
echo “<ul>”;
while($item = mysql_fetch_object($result))
{
// echo “parent = $parent id = $item->id”;
if ($orginal == $item->id) {
echo “<li> – <a href=getcat.php?id=”.$item->id.”><b>” . $item->namn . “</b></a> id: “.$item->id.”</li>”;
} else {
echo “<li> – <a href=getcat.php?id=”.$item->id.”>” . $item->namn . “</a> id: “.$item->id.”</li>”;
}
// echo “orignal = $orginal. id = $item->id . Parent = $parent”;
if ($orginal == $item->id && $item->id != $parent) {
displayChildren($item->id,$orginal);
}
}
echo “</ul>”;
}
function getmainid($parent)
{
$sql = mysql_query(“SELECT * FROM `kategorier` WHERE `id` = $parent”) or die (mysql_error());
while($sqlrow=mysql_fetch_object($sql)){
$nyttid = $sqlrow->parent_category;
if ($sqlrow->type == “huvud”) {
$id = $sqlrow->id;
} else {
$id = getmainid($nyttid);
}
}
return $id;
}
// Skriver ut själva menyn
$id = $_GET[“id”];
$menu = mysql_query(“SELECT * FROM `kategorier` WHERE `parent_category` = 0”) or die (mysql_error());
echo “<ul>”;
while($item = mysql_fetch_object($menu))
{
$currentid = getmainid($id);
if ($currentid == $item->id) {
echo “<li><a href=getcat.php?id=”.$item->id.”><b>” . $item->namn . “</b></a> id: “.$item->id.”</li>”;
}else {
echo “<li><a href=getcat.php?id=”.$item->id.”>” . $item->namn . “</a> id: “.$item->id.”</li>”;
}
if ($item->id != $currentid)
continue;
$**** = displayChildren($item->id,$id);
print_r ($****);
}
echo “</ul>”;[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@dk_zero-coolJun 22.2010 — If you want to make a menu tree style output, this can be done by a single simple function.

The database table "menu":
[CODE]id (int) (autoincrement)
subid (int)
name (varchar)
path (varchar)[/CODE]


The menu asambler function:
[code=php]function asamble_menu($subId=0) {
$html = "";
$result = mysql_query("SELECT * FROM menu WHERE subid=".$subId."");
if(mysql_num_rows() > 0) {
$html .= "<ul>";
while($row = mysql_fetch_assoc($result)) {
$html .= "<li>";
$html .= "<a href='".$row["path"]."'>".$row["name"]."</a>";
$html .= (( ($tmpHtml = asamble_menu($row["id"])) != "" ) ? "<div>".$tmpHtml."</div>" : "");
$html .= "</li>";
}
$html .= "</ul>";
}
return $html;
}

echo "<div>".asamble_menu()."</div>"[/code]


If this is not quite what you are looking for, please explain in better details what you whish to do.
Copy linkTweet thisAlerts:
@timtasticauthorJun 23.2010 — Well the output is one part of the problem. I want to be able to navigate by id.

For example :

SQL Table looks like this

ID NAME parent_category

1 some_name 0

2 some_name2 0

3 sub_category 1

4 sub_sub_category 3

and i want to navigate to Menu.php?id=4 (id 4 )

so lets say i'm in category (id) 4. How do i only display id 1 -> 3 -> 4 ?

And if im in : id 3. How do i display only category (3) and the subcategories under?. Hope you understand what i'm looking for. I want to be able to navigate in the menu by $_GET ["id"].
Copy linkTweet thisAlerts:
@MindzaiJun 23.2010 — This sort of thing is much easier to do, as well as more efficient in terms of database queries, if you use the Nested Set model instead of Adjacency List. This article explains how Nested Set is implemented.
Copy linkTweet thisAlerts:
@timtasticauthorJun 23.2010 — I've been reading through the link you gave my and it did not make me any more clever :-/. I understand i could do some query savings but the menu wont get that big that it affects stability and speed. The main problem is the php part to navigate and a bit of sql. Please help me to get this working. I have a deadline coming up.
×

Success!

Help @timtastic 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,
)...