i've been looking this over... i've tried piecing this together from the tutorial but not having any success - not gettting any response at all. any chance you have a sample file already put together I could get?
tx!
PHP related forum, discuss snippets, classes and tutorials
i've been looking this over... i've tried piecing this together from the tutorial but not having any success - not gettting any response at all. any chance you have a sample file already put together I could get?
tx!
I don't have a ready to go example, the tutorial is meant to explain how it works. What do you have?
hi Olaf! what I did was cut and pasted the code out into a php file... and set up my database connection... set up the test table...
i just need a sample to work from ... I can follow php code - but sometimes have trouble with the actual syntax and order of things... I could use anything that has the code where it should be etc in the file, I can format etc and take it from there..
if you can, it would be a big help!
I will help you but I don't have a complete file, let's start with the database, how does your table look like (incl. the records). Export the table with phpmyadmin and post it here.
thanks!!! I followed your instructions, so here is the sql
--
-- Table structure for table dyn_menu
--
CREATE TABLE dyn_menu (
id int(11) NOT NULL auto_increment,
label varchar(50) NOT NULL default '',
link_url varchar(100) NOT NULL default '#',
parent_id int(11) NOT NULL default '0',
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dumping data for table dyn_menu
--
INSERT INTO dyn_menu (id, label, link_url, parent_id) VALUES (1, 'Home', 'index.php', 0),
(2, 'Products', 'products.php', 0),
(3, 'Books', 'books.php', 2),
(4, 'Lamps', 'lamps.php', 2);
Okay looks good. Next create a php file, code the database connection and paste this code:
$sql = "SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY parent_id, id ASC"; $items = mysql_query($sql); while ($obj = mysql_fetch_object($items)) { if ($obj->parent_id == 0) { $parent_menu[$obj->id]['label'] = $obj->label; $parent_menu[$obj->id]['link'] = $obj->link_url; } else { $sub_menu[$obj->id]['parent'] = $obj->parent_id; $sub_menu[$obj->id]['label'] = $obj->label; $sub_menu[$obj->id]['link'] = $obj->link_url; $parent_menu[$obj->parent_id]['count']++; } } mysql_free_result($items); print_r($parent_menu); print_r($sub_menu);
Run this script on your webhost and post the output here.
here u go
Array (
[1] => Array ( [label] => Home [link] => index.php )
[2] => Array ( [label] => Products [link] => products.php [count] => 2 )
)
Array (
[3] => Array ( [parent] => 2 [label] => Books [link] => books.php )
[4] => Array ( [parent] => 2 [label] => Lamps [link] => lamps.php )
)...and what do you get if you replace the print_r statements with:
echo dyn_menu($parent_menu, $sub_menu, "menu", "nav", "subnav");
and add this function at the begin of your script as well:
function rebuild_link($link, $parent_var, $parent_val) { $link_parts = explode("?", $link); $base_var = "?".$parent_var."=".$parent_val; if (!empty($link_parts[1])) { $link_parts[1] = str_replace("&", "##", $link_parts[1]); $parts = explode("##", $link_parts[1]); $newParts = array(); foreach ($parts as $val) { $val_parts = explode("=", $val); if ($val_parts[0] != $parent_var) { array_push($newParts, $val); } } if (count($newParts) != 0) { $qs = "&".implode("&", $newParts); } return $link_parts[0].$base_var.$qs; } else { return $link_parts[0].$base_var; } }
i get a blank screen - nothing.
so your file should look like:
<?php
// your db connection is here
$sql = "SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY parent_id, id ASC";
$items = mysql_query($sql);
while ($obj = mysql_fetch_object($items)) {
if ($obj->parent_id == 0) {
$parent_menu[$obj->id]['label'] = $obj->label;
$parent_menu[$obj->id]['link'] = $obj->link_url;
} else {
$sub_menu[$obj->id]['parent'] = $obj->parent_id;
$sub_menu[$obj->id]['label'] = $obj->label;
$sub_menu[$obj->id]['link'] = $obj->link_url;
$parent_menu[$obj->parent_id]['count']++;
}
}
mysql_free_result($items);
function rebuild_link($link, $parent_var, $parent_val) {
$link_parts = explode("?", $link);
$base_var = "?".$parent_var."=".$parent_val;
if (!empty($link_parts[1])) {
$link_parts[1] = str_replace("&", "##", $link_parts[1]);
$parts = explode("##", $link_parts[1]);
$newParts = array();
foreach ($parts as $val) {
$val_parts = explode("=", $val);
if ($val_parts[0] != $parent_var) {
array_push($newParts, $val);
}
}
if (count($newParts) != 0) {
$qs = "&".implode("&", $newParts);
}
return $link_parts[0].$base_var.$qs;
} else {
return $link_parts[0].$base_var;
}
}
function dyn_menu($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "subnav", $extra_style = "foldout") {
$menu = "<ul id=\"".$main_id."\">\n";
foreach ($parent_array as $pkey => $pval) {
if (!empty($pval['count'])) {
$menu .= " <li><a class=\"".$extra_style."\" href=\"".$pval['link']."?".$qs_val."=".$pkey."\">".$pval['label']."</a></li>\n";
} else {
$menu .= " <li><a href=\"".$pval['link']."\">".$pval['label']."</a></li>\n";
}
if (!empty($_REQUEST[$qs_val])) {
$menu .= "<ul id=\"".$sub_id."\">\n";
foreach ($sub_array as $sval) {
if ($pkey == $_REQUEST[$qs_val] && $pkey == $sval['parent']) {
$menu .= "<li><a href=\"".rebuild_link($sval['link'], $qs_val, $sval['parent'])."\">".$sval['label']."</a></li>\n";
}
}
$menu .= "</ul>\n";
}
}
$menu .= "</ul>\n";
return $menu;
}
echo dyn_menu($parent_menu, $sub_menu, "menu", "nav", "subnav");
?>You must log in to post.