'Dynamic' PHP Navigation

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!

Moderator: General Moderators

ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

'Dynamic' PHP Navigation

Post by ra »

I have a navigation script that displays links, and when said links are clicked on, additional navigation options appear below the original links (and can take the user to another page at the same time). I am wondering if there is a way to have each set of additional links appear under the parent link? Also, how should I code the links to return to the appropriate page regardless of location in the site?

Code: Select all

<!--menu-->
<?
// Top main menu 
echo"<a href=\"http://homepage.com/index.php">Home</a> | ";
echo"<br /><a href=\"?sort=Service\">Service</a> | <br /><a href=\"?sort=search\">Search Engine</a> | <br /><a href=\"?sort=members\">Members</a>";

function index()
{
}
$choice=$_GET['sort'];

switch($choice)
{

// EX: Service submenu
case "Service":
echo "<br /><a href=\"http://scriptsinclude.com/En/\">Many usuful scripts at ScriptsInclude.com</a>";
break;// End Service submenu

//EX: search engine submenu
case "search":
echo "<br /><a href=\"http://google.com\">Google</a> | <a href=\"http://google.com/ads/\">Google Ads Sense</a><br /><a href=\"http://yahoo.com\">Yahoo!</a><br /><a href=\"http://msn.com\">Msn</a>";
break;// End search engine submenu

//EX: search engine submenu
case "members":
echo "<br /><a href=\"http://.com\">Logged in</a><br /><a href=\"http://.com/ads/\">Lost Password</a><br /><a href=\"http://.com\">Register!</a><br /><a href=\"http://.com\">Contact Us</a>";
break;// End search engine submenu

default:
index();
}

?>
</center>
<!--End menu-->
ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

Post by ra »

Anyone?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

This can be accomplished by using JS. PHP isn't in any way 'dynamic'.
ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

Post by ra »

i understand that php in not dynamic, but it seems as though this can be done. The current script works fine, i just need to place the sub-menu below its parent...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it'll require a page refresh, but it can be done... just have to output the submenu contextually from within your standard html output.
ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

Post by ra »

that model would work, as the parent link has a page associated with it.

button1 - folder/index.php
sub1 - folder/subpage1.php
sub2 - folder/subpage2.php
sub3 - folder/subpage3.php

What is my code missing to accomplish this?

Thanks.
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post by Calimero »

Realy don't understand why do it in php, JS is made for this.

And althouh I read your post 3 times, I havent understood what are you trying to accomplish:

It look's like a simple navigation menu - and when you refresh a page you just ( as you did ) Switch and echo the nececary links - in the table if it helps to make design better .

If this is not what you want - please write to my stupid brain what are you trying to do - it looks too simple ( to code something special for it )
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

understand what you're trying to do I do not. Trying to create a "dynamic" menu are you (like a JS menu)?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

menus like this
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

There are several ways to acheive this. I'd do it in one of two ways:

1) use a url param to identify which set of subs need to be shown (use it for all subs)
2) define a constant or variable on all of your subs and show the sub menu if the constant/variable is defined/set (my preferred method)

examples:

Code: Select all

&lt;style&gt;
td.subs
{
padding-left:4px; background-color:#somediffhexcolor;
}
&lt;/style&gt;
&lt;table width=&quote;100&quote;&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href=&quote;menuitem1.php?m1=1&quote;&gt;Menu Item 1&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href=&quote;menuitem2.php?m2=1&quote;&gt;Menu Item 2&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?if(isset($_GET&#1111;'m2'])){?&gt;
&lt;tr&gt;
&lt;td class=&quote;subs&quote;&gt;&lt;a href=&quote;sub2a.php?m2=1&quote;&gt;Sub Menu Item 2a&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quote;subs&quote;&gt;&lt;a href=&quote;sub2b.php?m2=1&quote;&gt;Sub Menu Item 2b&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?} // end if for sub 2s ?&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href=&quote;menuitem3.php?m3=1&quote;&gt;Menu Item 3&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
now the way that I'd prefer to do it:

Code: Select all

&lt;?
//define a constant for subs menu two (include this at the top of all of your sub2 pages)
define(&quote;SUBS2&quote;,1);
?&gt;
&lt;style&gt;
td.subs
{
padding-left:4px; background-color:#somediffhexcolor;
}
&lt;/style&gt;
&lt;table width=&quote;100&quote;&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href=&quote;menuitem1.php&quote;&gt;Menu Item 1&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href=&quote;menuitem2.php&quote;&gt;Menu Item 2&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?if(defined(&quote;SUBS2&quote;)){?&gt;
&lt;tr&gt;
&lt;td class=&quote;subs&quote;&gt;&lt;a href=&quote;sub2a.php&quote;&gt;Sub Menu Item 2a&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quote;subs&quote;&gt;&lt;a href=&quote;sub2b.php&quote;&gt;Sub Menu Item 2b&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?} // end if for sub 2s ?&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href=&quote;menuitem3.php&quote;&gt;Menu Item 3&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
ra
Forum Commoner
Posts: 58
Joined: Fri Mar 25, 2005 4:25 pm

Post by ra »

good talk; thanks everybody.
marike
Forum Newbie
Posts: 24
Joined: Thu Mar 31, 2005 6:19 pm
Location: New York

Dynamic PHP Navigation

Post by marike »

I personally don't see a need for PHP or Javascript for this menu. Why not just use CSS and the hover element. This way the menu looks the same even if Javascript is disable in users browser. As far as using Javascript for this, seems like a lot of code for something thats done with one line in CSS.

Unless you want to code it in PHP just 'cause you can?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, IE is fussy about hover (only lets it be used on A elements).
marike
Forum Newbie
Posts: 24
Joined: Thu Mar 31, 2005 6:19 pm
Location: New York

Post by marike »

Ambush Commander wrote:
"Well, IE is fussy about hover (only lets it be used on A elements)"

I'm not sure that's true, but...

Since it's a navigation menu, with links, which means <a href="blah">Link</a>,
isn't that what we're dealing with? A elements? And aren't there thousands of navigation menus that use CSS that work perfectly on IE?

I used alot of PHP to create this web site http://www.michaelarike.com from the gallery pages, database, email form. But for the menu I used CSS. You can make flashier menus with DHTML, but they won't be a accessible accross browsers.

I feel it's comes down to using the right tools for the task at hand.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Well I have a good reason for using Javascript for rollovers.

I download and stroee the images in memory when the <HEAD> loads. this means they respond instantly onMouseover instead of pausing while the new image downloads :P
Post Reply