php and unordered list navigation menu include

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
sparrow3274
Forum Newbie
Posts: 4
Joined: Fri Dec 15, 2006 4:20 pm

php and unordered list navigation menu include

Post by sparrow3274 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,

I was wondering if someone could help me out... I am making a navigation menu using css and unordered lists, the list is inside an include file and I have set up the list so it has 3 states (default, hover, and active) I am trying to have the code read the page_id(file name) and then have it generate id=current in the list... like <li id=current> but I keep getting all kinds of errors.... I can't put anything on the pages specifically because they are generated, so it has to go in the include...

Here's what i've messed up so far 

This is in the top:

Code: Select all

<?php
			if(!$_GET('page_id')) { $tab1 = 'id="current"'; }
			if(!$_GET('page_id') == 3) { $tab2 = 'id="current"'; } 
			if(!$_GET('page_id') == 4 ) { $tab3 = 'id="current"'; }  
?>
and here is my list navigation:

Code: Select all

<div id="navwrapper">
	  <ul id="nav">
	    <li$tab1><a href="<?php echo get_settings('home'); ?>/"><span>Home</span></a></li>
	    <li$tab2><a href="<?php echo get_settings('home'); ?>/Calendar/"><span>Calendar</span></a></li>
	    <li$tab3><a href="<?php echo get_settings('home'); ?>/?page_id=4"><span>Phonebook</span></a></li>
	    <li$tab4><a href="<?php echo get_settings('home'); ?>/Links/"><span>Links</span></a></li>
	    <li$tab5><a href="<?php echo get_settings('home'); ?>/Contact/"><span>Contact</span></a></li>
      </ul>
  </div>
does anyone know why this wouldn't work, or if there is a better way?
Thank you for any help you can provide
---


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: php and unordered list navigation menu include

Post by califdon »

Code: Select all

<?php
			if(!$_GET('page_id')) { $tab1 = 'id="current"'; }
			if(!$_GET('page_id') == 3) { $tab2 = 'id="current"'; } 
			if(!$_GET('page_id') == 4 ) { $tab3 = 'id="current"'; }  
?>
and here is my list navigation:

Code: Select all

<div id="navwrapper">
	  <ul id="nav">
	    <li$tab1><a href="<?php echo get_settings('home'); ?>/"><span>Home</span></a></li>
	    <li$tab2><a href="<?php echo get_settings('home'); ?>/Calendar/"><span>Calendar</span></a></li>
	    <li$tab3><a href="<?php echo get_settings('home'); ?>/?page_id=4"><span>Phonebook</span></a></li>
	    <li$tab4><a href="<?php echo get_settings('home'); ?>/Links/"><span>Links</span></a></li>
	    <li$tab5><a href="<?php echo get_settings('home'); ?>/Contact/"><span>Contact</span></a></li>
      </ul>
  </div>
---[/quote]
Try inserting a space between <li and the $tab..
sparrow3274
Forum Newbie
Posts: 4
Joined: Fri Dec 15, 2006 4:20 pm

Post by sparrow3274 »

i tried that... but it didn't work... this is what I got

Fatal error: Call to undefined function: array() in

Thanks for your help
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_GET is not a function, it is an array.

Code: Select all

$_GET['foo']
// NOT
$_GET('foo')
sparrow3274
Forum Newbie
Posts: 4
Joined: Fri Dec 15, 2006 4:20 pm

Post by sparrow3274 »

Thank you... that stopped it from blowing up, but it still doesn't make the tab navigation active, when it is on that page.... any suggestions?

The code now looks like:

Code: Select all

<?php
			if(!$_GET['page_id']) { $tab1 = 'id="current"'; }
			if(!$_GET['page_id'] == 3) { $tab2 = 'id="current"'; } 
			if(!$_GET['page_id'] == 4 ) { $tab3 = 'id="current"'; }  
?>
and the html looks like:

Code: Select all

<div id="navwrapper">
	  <ul id="nav">
	    <li $tab1><a href="<?php echo get_settings('home'); ?>/"><span>Home</span></a></li>
	    <li $tab2><a href="<?php echo get_settings('home'); ?>/Calendar/"><span>Calendar</span></a></li>
	    <li $tab3><a href="<?php echo get_settings('home'); ?>/?page_id=4"><span>Phonebook</span></a></li>
	    <li $tab4><a href="<?php echo get_settings('home'); ?>/Links/"><span>Links</span></a></li>
	    <li $tab5><a href="<?php echo get_settings('home'); ?>/Contact/"><span>Contact</span></a></li>
      </ul>
 </div>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$tab1 et al are outside of PHP, therefore not processed by it. You need to echo them.
sparrow3274
Forum Newbie
Posts: 4
Joined: Fri Dec 15, 2006 4:20 pm

Post by sparrow3274 »

makes sense ---

it is almost there: the first tab highlights... but none of the other pages highlight when i go to them... any other suggestions?

The code now looks like:

Code: Select all

<?php
         if(!$_GET['page_id']) { $tab1 = 'id="current"'; }
         if(!$_GET['page_id'] = 3) { $tab2 = 'id="current"'; } 
         if(!$_GET['page_id'] = 4 ) { $tab3 = 'id="current"'; }  
?>
the end of the url for the first one looks like /family/ ---no page name and the correct tab highlights --

the end of the url for the third one looks like /family/?page_id=4 ---no tab is highlighted --

list:

Code: Select all

<div id="navwrapper">
     <ul id="nav">
       <li <?php  echo "$tab1";  ?>><a href="<?php echo get_settings('home'); ?>/"><span>Home</span>[/url]</li>
       <li <?php  echo "$tab2"; ?>><><a href="<?php echo get_settings('home'); ?>/Calendar/"><span>Calendar</span>[/url]</li>
       <li <?php  echo "$tab3"; ?>><a href="<?php echo get_settings('home'); ?>/?page_id=4"><span>Phonebook</span>[/url]</li>
--- thanks for your help
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

With only one equal sign PHP understands that as an assignment.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Lets try this in comment form...

Code: Select all

<?php
         // if $_GET['page_id'] is false the $tab1 becomes 'id = "current"'
         if(!$_GET['page_id']) { $tab1 = 'id="current"'; }

         // if the opposite of $_GET['page_id'] is now set to 3 (THIS IS ABSOLUTELY THE WRONG SYNTAX)
         if(!$_GET['page_id'] = 3) { $tab2 = 'id="current"'; }

         // if the opposite of $_GET['page_id'] is now set to 3 (THIS IS ABSOLUTELY THE WRONG SYNTAX)
         if(!$_GET['page_id'] = 4 ) { $tab3 = 'id="current"'; } 
?>
Maybe you can do something along the lines of

Code: Select all

<?php
// Read the get param into the var
$page_id = isset($_GET['page_id']) ? $_GET['page_id'] : 0;

// Defaults
$tab1 = '';
$tab2 = '';
$tab3 = '';

if ($page_id == 4)
{
  $tab3 = ' id="current"';
} 
elseif ($page_id == 3)
{
  $tab2 = ' id="current"';
}
else
{
  $tab1 = ' id="current"';
}
?>
Post Reply