Page 1 of 1

php and unordered list navigation menu include

Posted: Fri Dec 15, 2006 4:39 pm
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]

Re: php and unordered list navigation menu include

Posted: Fri Dec 15, 2006 5:02 pm
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..

Posted: Fri Dec 15, 2006 5:13 pm
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

Posted: Fri Dec 15, 2006 5:42 pm
by feyd
$_GET is not a function, it is an array.

Code: Select all

$_GET['foo']
// NOT
$_GET('foo')

Posted: Fri Dec 15, 2006 6:00 pm
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>

Posted: Fri Dec 15, 2006 6:04 pm
by feyd
$tab1 et al are outside of PHP, therefore not processed by it. You need to echo them.

Posted: Fri Dec 15, 2006 7:06 pm
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

Posted: Fri Dec 15, 2006 11:30 pm
by feyd
With only one equal sign PHP understands that as an assignment.

Posted: Sat Dec 16, 2006 8:30 am
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"';
}
?>