ARGH! URL Vars & My Menu System! (Prob Obvious ans.)

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

Post Reply
mhulse
Forum Commoner
Posts: 41
Joined: Fri Jun 11, 2004 12:50 am

ARGH! URL Vars & My Menu System! (Prob Obvious ans.)

Post by mhulse »

TITLE: ARGH! URL Vars & My Menu System! (Prob Obvious ans.)

Hi folks,

My dynamic menu code:

Code: Select all

<div id="container">
<ul id="menu">
<?php

    ##########################
    ##  navigation.inc.php  ##
    ##############################################################################
	##  Use this code to display a smart/dynamic PHP/CSS menu on your website:  ##
    ##############################################################################
	
///////////////////////////////////////// function crtLinks():
///////////////////////////////////////// This function will create links and add the "current" CSS class to the current page link by comparing "$page" and "$pages[$link]":
function crt_links($link) {

	# Create array and pop with items to be stripped from URL:
	$locate = array("/folio/", "index.php");
	
	# $pages pops an array with links set by webmaster:
	$pages = array (
					"Home" => "index.php",
					"About" => "index.php?display=about",
					"Work" => "index.php?display=work",
					"Resume" => "index.php?display=resume",
					"Code" => "index.php?display=code",
					"Contact" => "index.php?display=contact",
					"Forum" => "index.php?display=forum",
					"Guestbook" => "index.php?display=guestbook",
					"Links" => "index.php?display=links",
					"Dotcoms" => "index.php?display=dotcoms"
					); // Add more links here.
	
	# $titles pops an array with link-related titles set by webmaster:
	$titles = array (
					"Home" => "[Ambiguism -- Home]",
					"About" => "[Ambiguism -- About]",
					"Work" => "[Ambiguism -- Work]",
					"Resume" => "[Ambiguism -- Resume]",
					"Code" => "[Ambiguism -- Code]",
					"Contact" => "[Ambiguism -- Contact]",
					"Forum" => "[Ambiguism -- Forum]",
					"Guestbook" => "[Ambiguism -- Guestbook]",
					"Links" => "[Ambiguism -- Links]",
					"Dotcoms" => "[Ambiguism -- Dotcoms]"
					); // Add more titles here.
					
	# Find the current page(s) url:
	$page = str_replace($locate[0], "", $_SERVER['SCRIPT_NAME']) . str_replace($locate, "", $_SERVER['REQUEST_URI']);

	# Make $link lower-case for style-sheet "id" reference:
	$lower =  strtolower($link);

	# Compare URL to links for display of current and non-current nav links:
	if($page == $pages[$link]) { // If page is current page, set CSS class to "selected":
		echo '<li id="'.$lower.'"><a class="selected" href="'.$pages[$link].'" title="'.$titles[$link].'">'.$link.'</a></li>'."\n";
	} else { // Set to non-current style
		echo '<li id="'.$lower.'"><a href="'.$pages[$link].'" title="'.$titles[$link].'">'.$link.'</a></li>'."\n";
	}
}

# Call crtLinks() function and display the links on the page:
crt_links("Home");
crt_links("About");
crt_links("Work");
crt_links("Resume");
crt_links("Code");
crt_links("Contact");
crt_links("Forum");
crt_links("Guestbook");
crt_links("Links");
crt_links("Dotcoms"); // Add links here to reflect abave link/title list.
?>
</ul>
</div>
My problem:

The above script works great if the URL looks like this:

Code: Select all

http://www.myDomain.com/folio/index.php?display=work
But, once I start doing this:

Code: Select all

http://www.myDomain.com/folio/index.php?display=work&amp;test=3d_study&amp;img=1
... the script fails to add "selected" CSS class to current page link. :(

I need help.... how can I make my code ignore everything in the URL starting with "&" on up? Or, can anyone suggest a better way to compare the array elements to URL?

Hopefully that makes sense... Also, any feedback on the code would be appreciated... Is there a way I could make my script more compact/execute faster?

Thanks a billion in advance all!

Cheers
Micky
mhulse
Forum Commoner
Posts: 41
Joined: Fri Jun 11, 2004 12:50 am

Post by mhulse »

Hi,

I was able to solve my problem by doing this:

Code: Select all

# If & is found in string $page, put it and everything after it in var $url:
	$url = strstr($page, '&');
	
	# Compare URL to links for display of current and non-current nav links (remove var $url from $page for comparison):
	if($pages[$link] == (str_replace($url, "", $page))) { // If page is current page, set CSS class to "selected":
Maybe not the best way, it still seems slow... but, it works, and I am happy...

I am still open to other suggestions.

Thanks!
Cheers
Micky
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I'm sure your not looking at optimizing, but I would recommend just having one array

Code: Select all

<?php
$pages = array (
"Home" => array("index.php","[Ambiguism -- Home]"),
"About" => array("index.php?display=about","[Ambiguism -- About]"),
"Work" => array("index.php?display=work","[Ambiguism -- Work]"),
"Resume" => array("index.php?display=resume","[Ambiguism -- Resume]"),
"Code" => array("index.php?display=code","[Ambiguism -- Code]"),
"Contact" => array("index.php?display=contact","[Ambiguism -- Contact]"),
"Forum" => array("index.php?display=forum","[Ambiguism -- Forum]"),
"Guestbook" => array("index.php?display=guestbook","[Ambiguism -- Guestbook]"),
"Links" => array("index.php?display=links","[Ambiguism -- Links]"),
"Dotcoms" => array("index.php?display=dotcoms", "[Ambiguism -- Dotcoms]"),
); 
?>
mhulse
Forum Commoner
Posts: 41
Joined: Fri Jun 11, 2004 12:50 am

Post by mhulse »

Oooooooh, nice, yeah, I am totally looking to optomize... Thanks Phenom! You rock! :D
Post Reply