multi level dynamic menu whats the best way to go about it??
09-24-2007, 05:42 PM
|
multi level dynamic menu whats the best way to go about it??
|
Posts: 6,521
Name: Dan
Location: Swindon
|
Hi,
Im trying to build a multi level navigation, i need to be able to control the order of it, have sub levels (ie Flyout) etc...
im heading with mysql to control it but the more i think about how to do it the more confussed im making myself..
Thanks, TP for all good answers 
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
09-24-2007, 06:02 PM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 350
|
I can't anymore than look into using CSS. It is valid; and can look pretty cool.
OR
http://dhtml-menu.com/samples/sample311.html
|
|
|
|
09-25-2007, 07:50 AM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 6,521
Name: Dan
Location: Swindon
|
Sorry probably my fault. not what i ment
basically i need a way to have a dynamic menu which would turn out like
Code:
<ul>
<li><a href="url">Home link</a></li>
<li>
<ul>
<li>Cat one</li>
<li><a href="url1">Cat one link</a></li>
</ul>
</ul>
but i want it controlled by mysql, so if i installed a module and i had it active etc it would add some code to my database and if it required multiple pages it would add a <ul> with all te sub pages which would show on page.
but i also want the ability to have a admin where i can change order and stuff?
Ok think Wordpress here, if you have used it you will know what i mean where oyu can easily change the order of the bits and show links not show links etc etc..
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
09-25-2007, 10:58 AM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 483
|
Are you talking more 'DB schema' or 'PHP code'?
As far as the schema is concerned, you would just do something like: link_id, link_parent, link_name, link_url. Set parent to 0 if it's a 'root' item, otherwise set it to the link ID of the parent item.
As far as PHP code goes, you would pull all of the menu items out of the DB, set them into an array where the key is the item ID and the value is an array of parent ID, link name and link URL.
Then, have a function similar to:
PHP Code:
function display_menu_items( $item_array, $parent_id = 0 ) {
foreach( $item_array as $item_id => $item_information ) {
if ( $item_information[ 'parent_id' ] == $parent_id ) {
print $item_information[ 'link_name' ];
$item_array_copy = $item_array;
reset( $item_array_copy );
display_menu_items( $items_array_copy, $item_id );
}
}
}
That code is off the top of my head so no guarantees that it will run, but you should get the idea, at least. Naturally you would want to change that print line to something useful.
Hopefully that's what you wanted 
|
|
|
|
09-25-2007, 11:37 AM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 6,521
Name: Dan
Location: Swindon
|
well i was thinking along the same sort of lines with the db schema but i also was using a cat_id table which i was thinking for the ones which had sub
cats.
thanks for the PHP gives me a good starting point.
But what would be the best way to control the display order?
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
09-25-2007, 11:52 AM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 483
|
Oh yeah, I forgot about that
In your DB schema just have a 'display_order' value. In theory, if you sort by that display order when pulling them from the DB you should be able to display them properly (even though they'll all be mixed up, the for loop above should be able to cut through it all).
Now as far as SETTING the display order, well, that's up to you. I would do as Joomla does and show the relevant items each with a textbox (for just typing in the new order number) as well as an up arrow and a down arrow that, when clicked, will essentially increment or decrement the order number before saving the new values.
|
|
|
|
09-25-2007, 01:02 PM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 6,521
Name: Dan
Location: Swindon
|
god i have to actually try and write this now @_@
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
09-25-2007, 01:11 PM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 6,521
Name: Dan
Location: Swindon
|
Just thinking could would it be better to have it in one table and have a is_cat field which is either 1 or 0 ??
God why does this confuse me so.?
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
09-25-2007, 02:25 PM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 6,521
Name: Dan
Location: Swindon
|
Ok i give up IM STUPID!
how the hell do i do the sql queries for this im so confussed!
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
09-25-2007, 04:00 PM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 483
|
Well, I can't really tell you what query you need until you tell me what your DB structure is
While you're at it... are you going to want to allow category 'nesting'? ie. can a subcategory have a subcategory of its own? Also, can a subcategory 'heading' be a link in and of itself?
|
|
|
|
09-25-2007, 04:11 PM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 6,521
Name: Dan
Location: Swindon
|
Ok,
Erm sod sub sub cats just 2 levels i cant be botherd with that.
erm ok well i guess i would like the cats to be links as well but not always.
and im not sure what would be best for schema.
at the moment i have this:
Because im lazy heres the export of one of the tables i made
CREATE TABLE `navi_links` (
`link_id` smallint(6) NOT NULL auto_increment,
`link_cat` smallint(6) NOT NULL,
`link_url` varchar(255) collate latin1_general_ci NOT NULL,
`link_name` varchar(255) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`link_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=2 ;
and here the cat table i made
CREATE TABLE `navi_cat` (
`navi_cat_id` smallint(6) NOT NULL auto_increment,
`navi_cat_name` varchar(255) collate latin1_general_ci NOT NULL,
`navi_cat_order` smallint(6) NOT NULL,
PRIMARY KEY (`navi_cat_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;
but im thinking maybe it would be better to have it as one and have a like is_cat field of something??
Im really like confuddled
Thanks alot for help 
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
09-25-2007, 04:53 PM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 483
|
ASSUMING the navi_cat table is ONLY used for 'categorising' the menu items AND ASSUMING that these categories are just for grouping them into one massive menu, then I would be inclined to put them into a single table.
If you wanted the categories themselves to be links sometimes, but not always, then I would just have a special case where if the URL is empty, just show the name. Therefore, 'sometimes a link, sometimes not' is nothing really special.
So again, so long as the category is not related to content at all (just the menu) then I would still make the table as I mentioned above. Something like:
CREATE TABLE `navi_links` (
`link_id` smallint(6) NOT NULL auto_increment,
`link_parent` smallint(6) NOT NULL,
`link_url` varchar(255) collate latin1_general_ci NOT NULL,
`link_name` varchar(255) collate latin1_general_ci NOT NULL,
`display_order` smallint(6) NOT NULL,
PRIMARY KEY (`link_id`)
);
Pretty much as you had it, though adding the display order and changing the link_cat value to a parent_id (which will point to another entry in the same table, as opposed to an entry in another table).
So now, let me try some more code... I'll base this on the function I wrote before, but I will add the 'possibly a link, possibly not' option as well as indenting based on subcategory level and also adding a '<br />'.
PHP Code:
function display_menu_items( $item_array, $parent_id = 0, $cat_depth = 0 ) {
foreach( $item_array as $item_id => $item_information ) {
if ( $item_information[ 'parent_id' ] == $parent_id ) {
$link_text = '';
for ($i = 0; ( $i < $cat_depth ); $i++ ) {
$link_text .= ' ';
}
if ( $item_information[ 'link_url' ] != '' ) {
$link_text .= '<a href="' . $item_information[ 'link_url' ] . '">';
}
$link_text .= $item_information[ 'link_name' ];
if ( $item_information[ 'link_url' ] != '' ) {
$link_text .= '</a>';
}
$link_text .= '<br />';
print $link_text;
$item_array_copy = $item_array;
reset( $item_array_copy );
display_menu_items( $items_array_copy, $item_id, ( $cat_depth + 1 ) );
}
}
}
So to use that function you would just do a SELECT * on the menu table and get the results into an associative array with mysql_fetch_array(...). When you get the result, move the data around a little so that we can access all of the information via the link ID (in the following example, we will dump data into the variable named $menu_array). Finally, just call the function above.
Something like:
PHP Code:
$query = 'SELECT * FROM `navi_links` ORDER BY `display_order`;';
$result = mysql_query( $query );
$menu_array = array();
while ( $row = mysql_fetch_array( $result ) ) {
$menu_array[ $row[ 'link_id' ] ] = $row;
}
display_menu_items( $menu_array );
If you want to have more than one overall menu on the same page, then I would suggest simply adding a 'menu_id' column to the 'navi_links' table and then adding a WHERE clause to your initial MySQL query, depending on the menu you are trying to display.
Hopefully that all makes some sense and helps!
|
|
|
|
09-25-2007, 04:58 PM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 6,521
Name: Dan
Location: Swindon
|
Ok ima bit confused but im gonna have a bash...
sow will this make the menu in the format of
<ul>
<li>Link</li>
<ul>
<li>Sub cat</li>
<li>suvfd</li>
</ul>
<li>link</li>
</ul>
???
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
09-25-2007, 05:04 PM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 6,521
Name: Dan
Location: Swindon
|
i did it and i got a invalid argument for foreach on line six o.0
ok heres what i put in DB
CREATE TABLE `navi_links` (
`link_id` smallint(6) NOT NULL auto_increment,
`link_parent` smallint(6) NOT NULL,
`link_url` varchar(255) collate latin1_general_ci NOT NULL,
`link_name` varchar(255) collate latin1_general_ci NOT NULL,
`display_order` smallint(6) NOT NULL,
PRIMARY KEY (`link_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=4 ;
--
-- Dumping data for table `navi_links`
--
INSERT INTO `navi_links` (`link_id`, `link_parent`, `link_url`, `link_name`, `display_order`) VALUES (1, 0, 'http://localhost/index.php', 'Home', 1);
INSERT INTO `navi_links` (`link_id`, `link_parent`, `link_url`, `link_name`, `display_order`) VALUES (2, 1, 'http://localhost/modules/articlemanager/index.php', 'Research', 2);
INSERT INTO `navi_links` (`link_id`, `link_parent`, `link_url`, `link_name`, `display_order`) VALUES (3, 1, 'http://localhost/modules/articlemanager/admin.php', 'admin', 0);
and heres the code im using...
(at the moment its just in one file)
PHP Code:
<?php include ('includes.php'); function display_menu_items( $item_array, $parent_id = 0, $cat_depth = 0 ) { foreach( $item_array as $item_id => $item_information ) { if ( $item_information[ 'parent_id' ] == $parent_id ) { $link_text = ''; for ($i = 0; ( $i < $cat_depth ); $i++ ) { $link_text .= ' '; } if ( $item_information[ 'link_url' ] != '' ) { $link_text .= '<a href="' . $item_information[ 'link_url' ] . '">'; } $link_text .= $item_information[ 'link_name' ]; if ( $item_information[ 'link_url' ] != '' ) { $link_text .= '</a>'; } $link_text .= '<br />'; print $link_text; $item_array_copy = $item_array; reset( $item_array_copy ); display_menu_items( $items_array_copy, $item_id, ( $cat_depth + 1 ) ); } } }
$query = 'SELECT * FROM `navi_links` ORDER BY `display_order`;'; $result = mysql_query( $query ); $menu_array = array(); while ( $row = mysql_fetch_array( $result ) ) { $menu_array[ $row[ 'link_id' ] ] = $row; } display_menu_items( $menu_array ); ?>
whats wrong?
and it just like shows three links 
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
09-25-2007, 07:40 PM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 483
|
Are you sure it wasn't in line 6 of your includes.php file? I ask this because unless you have whitespace before your opening PHP tag, it's pretty plain to see that my foreach is NOT on line 6 in the first place.
Look, the hard part is done. If you can't work out the formatting code for it, well... you know my stance on thinking for yourself.
|
|
|
|
09-26-2007, 10:03 AM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 6,521
Name: Dan
Location: Swindon
|
well i dont have any foreach in the included files..
I know sorry i am being very annouying 
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
09-26-2007, 10:26 AM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 483
|
OK, so instead of paraphrasing the error message, copy and paste it. Then attach the actual file that you're using... that way we can be sure that nothing got lost in the copy and paste.
But before you do that, try this:
Just before calling display_menu_items( $menu_array ); add:
print_r( $menu_array );
and tell me what the output is. If the array is empty, that may be causing the error.
If it's empty, then something's wrong with the MySQL portion. Maybe you need to connect to your DB first, maybe there's something wrong with my MySQL code. I don't know... try mysql_error() to find out.
Dan, you're not being annoying, you're just not willing to go out and work out what the problem is yourself. You should be able to look at my code and understand it enough to be able to rewrite it to work if need be. If you can't do that, well, I end up back were I was with you a while ago  . I don't really care if you can't rewrite it, but tell me: what did you do to try and fix this yourself before coming back and saying "it doesn't work"? Your answer to that is what drives me to either help or give up...
|
|
|
|
09-26-2007, 11:42 AM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 6,521
Name: Dan
Location: Swindon
|
foreach is on line 6.. vbulletin compresses code lines when submitted i have a few new lines in there normally.
heres the page. errors.
Code:
admin
Warning: Invalid argument supplied for foreach() in M:\Server\xampp\htdocs\cms1\includes\navi.php on line 6
Home
Warning: Invalid argument supplied for foreach() in M:\Server\xampp\htdocs\cms1\includes\navi.php on line 6
Research
Warning: Invalid argument supplied for foreach() in M:\Server\xampp\htdocs\cms1\includes\navi.php on line 6
I am going to give it a bash at figuring this out and tring to get it how i need 
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
Last edited by dansgalaxy; 09-26-2007 at 11:44 AM..
|
|
|
|
09-26-2007, 12:26 PM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 483
|
Well that makes more sense... new lines suck, though, for header reasons, but anyway.
Don't forget to look into this:
Quote:
|
Originally Posted by TwistMyArm
But before you do that, try this:
Just before calling display_menu_items( $menu_array ); add:
print_r( $menu_array );
and tell me what the output is. If the array is empty, that may be causing the error.
If it's empty, then something's wrong with the MySQL portion. Maybe you need to connect to your DB first, maybe there's something wrong with my MySQL code. I don't know... try mysql_error() to find out.
|
|
|
|
|
09-26-2007, 12:36 PM
|
Re: multi level dynamic menu whats the best way to go about it??
|
Posts: 6,521
Name: Dan
Location: Swindon
|
Okay, but if it wasnt connecting to the sql or there was a mysql error wouldnt it not show the links in the first place?
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
|
« Reply to multi level dynamic menu whats the best way to go about it??
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|