|  Topic awaiting preservation: Simple PHP question (Page 1 of 1)  | |
|---|---|
| Paranoid (IV) Inmate From: Lost Angeles Kalifornia, via Hawaii.... |  posted 09-23-2008 22:20 I'm in the process of trying to setup a menu that reflects a certain style if the current page is active. Here is the code for that part... | 
| Lunatic (VI) Inmate From: under the bed |  posted 09-24-2008 00:33 you have an "if", now you need an "else" code: if ($currentPage == 'master.php') {
$homelink = "<li class="active">Home</li>";
}
else {
$homelink = "<li><a href="master.php">Home</a></li>";
}
 code: <?php echo $homelink; ?> 
 | 
| Paranoid (IV) Inmate From: Lost Angeles Kalifornia, via Hawaii.... |  posted 09-24-2008 00:56 As usual DL you are a Godsend! Thanks so much. | 
| Paranoid (IV) Inmate From: Lost Angeles Kalifornia, via Hawaii.... |  posted 09-24-2008 01:18 ok... tried it but got an error.... | 
| Maniac (V) Mad Librarian From: Seoul, Korea |  posted 09-24-2008 02:35 Grrr... | 
| Maniac (V) Mad Librarian From: Seoul, Korea |  posted 09-24-2008 02:50 [Edit 1: OK, let's try this again. Apologies for making everyone scroll way to the right, but the only way I could prevent the numbers in square brackets from turning into links was by using the "code" tags. And no, unchecking the "Enable linkwords" checkbox before posting did absolutely nothing. Someone needs to look into that.] code: <?php
$menu_link = array("<a href=\"index.php\">Home</a>", 
"<a href=\"about.php\">About</a>", 
"<a href=\"sermons.php\">Sermons</a>", 
"<a href=\"articles.php\">Articles</a>", 
"<a href=\"gallery.php\">Gallery</a>",
"<a href=\"churches.php\">Churches</a>",
"<a href=\"cirectory.php\">Directory</a>",
"<a href=\"contact.php\">Contact</a>")
switch($currentPage)
	{
	case "index.php":
		$menu_item[0] = "Home";
		break;
	case "about.php":
		$menu_item[1] = "About";
		break;
	case "sermons.php":
		$menu_item[2] = "Sermons";
		break;
	case "articles.php":
		$menu_item[3] = "Articles";
		break;
	case "gallery.php":
		$menu_item[4] = "Gallery";
		break;
	case "churches.php":
		$menu_item[5] = "Churches";
		break;
	case "directory.php":
		$menu_item[6] = "Directory";
		break;
	case "contact.php":
		$menu_item[7] = "Contact";
		break;
	}
?>
(The following is within the HTML document, obviously)
<div id="menu">
     <ul>
          <li><?php echo($menu_item[0]); ?></li>
          <li><?php echo($menu_item[1]); ?></li>
          <li><?php echo($menu_item[2]); ?></li>
          <li><?php echo($menu_item[3]); ?></li>
          <li><?php echo($menu_item[4]); ?></li>
          <li><?php echo($menu_item[5]); ?></li>
          <li><?php echo($menu_item[6]); ?></li>
          <li><?php echo($menu_item[7]); ?></li>
     </ul>
</div>
 | 
| Paranoid (IV) Inmate From: Lost Angeles Kalifornia, via Hawaii.... |  posted 09-24-2008 03:56 So let me make sure I get this right... | 
| Maniac (V) Mad Librarian From: Seoul, Korea |  posted 09-24-2008 09:42 You would get an unexpected T_SWITCH error if the variable in the switch statement  is not defined. | 
| Maniac (V) Mad Scientist with Finglongers From: Germany |  posted 09-24-2008 09:59 now, you'd get the 'unexpected T_SWITCH' if you're missing a ; after the array(...). code: <div id="menu">
     <ul>
          <li><?php echo($menu_item[0]); ?></li>
          <li><?php echo($menu_item[1]); ?></li>
          <li><?php echo($menu_item[2]); ?></li>
          <li><?php echo($menu_item[3]); ?></li>
          <li><?php echo($menu_item[4]); ?></li>
          <li><?php echo($menu_item[5]); ?></li>
          <li><?php echo($menu_item[6]); ?></li>
          <li><?php echo($menu_item[7]); ?></li>
     </ul>
</div>
 | 
| Maniac (V) Mad Librarian From: Seoul, Korea |  posted 09-24-2008 10:08 Ah, did I forget the semicolon after the array? ...it would appear that I did. Always check the semicolons... | 
| Maniac (V) Mad Scientist with Finglongers From: Germany |  posted 09-24-2008 17:28 efficent for the programmer, at least. | 
| Maniac (V) Inmate From: there...no..there..... |  posted 09-24-2008 22:06 also, the array needs to be $menu_item not $men_link.  Good stuff Suho. | 
| Paranoid (IV) Inmate From: Norway |  posted 09-24-2008 22:14 On a site I was maintaining I used: code: // $scriptNameNavEquivalent is the name of the script requested $navArray = array ( "home" => "home.php", "articles" => "articles.php", "random" => "random.php", "links" => "links.php", "faq" => "faq.php", "goodies" => "goodies.php", "contact" => "contact.php", ); echo "<ul>"; foreach( $navArray as $currentNavItem => $currentNavItemUrl ) echo "\t\t\t<li><a href='". $currentNavItemUrl ."'". ($currentNavItem==$scriptNameNavEquivalent?" class='selected'":"") .">". $currentNavItem ."</a></li>\n"; echo "</ul>"; | 
| Maniac (V) Mad Librarian From: Seoul, Korea |  posted 09-25-2008 02:01 quote: 
 | 
| Paranoid (IV) Inmate From: Lost Angeles Kalifornia, via Hawaii.... |  posted 09-25-2008 08:55 Wow guys I really appreciate all the input.  Just got in, it's about Midnight so I'll get to tweaking tomorrow morning.  But big thanks for the contributions!  May not seem like alot to you guys, but for me it's HUUUGE! | 
| Maniac (V) Mad Librarian From: Seoul, Korea |  posted 09-25-2008 10:27 Just make sure that, if you use my code (or a variation thereof), you take into account the subsequent corrections to all the stupid little mistakes I made. | 
| Paranoid (IV) Inmate From: Lost Angeles Kalifornia, via Hawaii.... |  posted 09-25-2008 20:11 It works now, but how would I disable the link if you're on the current page?  I was told that best practice would be to have the link not be active.  If it's a pain, I'm not going to worry about it, but if it's relatively simple then I'll go for it. | 
| Maniac (V) Inmate From: there...no..there..... |  posted 09-25-2008 23:07 I'm not sure why that would be the "best practice".  If anything, I like to set the current state of the menu according to what page they are on. | 
| Paranoid (IV) Inmate From: Lost Angeles Kalifornia, via Hawaii.... |  posted 09-26-2008 03:15 The current state works, I'm fine with it being active, but I thought it optimum to deactivate current page link so it doesnt link to itself. thats all.  :-) | 
| Lunatic (VI) Inmate From: under the bed |  posted 09-26-2008 04:36 Nope, I would say not optimum. |