Jump to content


Photo

simple elseif problem


  • Please log in to reply
3 replies to this topic

#1 aebstract

aebstract
  • Members
  • PipPipPip
  • Advanced Member
  • 1,105 posts

Posted 06 October 2006 - 02:52 AM

I really didn't want to have to ask for help but it's late, and I can't seem to find the problem.. which is retarded. It's probably something dumb.

I have a very simple elseif statement for my tab navigation (since I can't think of another way to set the active tab). All it has to do is take the variable from the url and check through the if statements, finding the correct one and bam. For some reason it doesn't want to work, can anyone take a look at my code and the site and tell me whats going on? Thanks in advance!

http://www.carbenco.com/uy/

<?php


if ($page == about) {
echo '  		     <ul id="navlist">
        				<li><a href="index.php">Home</a></li>
        				<li class="active"><a href="index.php?page=about">About</a></li>
        				<li><a href="index.php?page=gallery">Gallery</a></li>
        				<li><a href="index.php?page=contact">Contact</a></li>
    				</ul>';
} elseif ($page == gallery) {
echo '  		     <ul id="navlist">
        				<li><a href="index.php">Home</a></li>
        				<li><a href="index.php?page=about">About</a></li>
        				<li class="active"><a href="index.php?page=gallery">Gallery</a></li>
        				<li><a href="index.php?page=contact">Contact</a></li>
    				</ul>';
} elseif ($page == contact) {
echo '  		     <ul id="navlist">
        				<li><a href="index.php">Home</a></li>
        				<li><a href="index.php?page=about">About</a></li>
        				<li><a href="index.php?page=gallery">Gallery</a></li>
        				<li class="active"><a href="index.php?page=contact">Contact</a></li>
    				</ul>';
} else {
echo '  		     <ul id="navlist">
        				<li class="active"><a href="index.php">Home</a></li>
        				<li><a href="index.php?page=about">About</a></li>
        				<li><a href="index.php?page=gallery">Gallery</a></li>
        				<li><a href="index.php?page=contact">Contact</a></li>
    				</ul>';
}


?>

There is an area of the mind that could be called unsane, beyond sanity, and yet
not insane. Think of a circle with a fine split in it. At one end there's
insanity. You go around the circle to sanity, and on the other end of the
circle, close to insanity, but not insanity, is unsanity.


#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 06 October 2006 - 02:58 AM

much more effective ways of doing this:

<?php
echo '<ul id="navlist">
		<li'.(($page == 'home' || empty($page)) ? ' class="active"' : '').'><a href="index.php">Home</a></li>
		<li'.(($page == 'about') ? ' class="active"' : '').'><a href="index.php?page=about">About</a></li>
		<li'.(($page == 'gallery') ? ' class="active"' : '').'><a href="index.php?page=gallery">Gallery</a></li>
		<li'.(($page == 'contact') ? ' class="active"' : '').'><a href="index.php?page=contact">Contact</a></li>
	</ul>';
?>

the reason yours didn't work is most likely because you aren't encasing your comparison string (ie. "about" etc.) in single quotes.  without using quotes, PHP thinks that the comparison is a constant, not a string.

look up strings and the ternary operator, which i've used here.

#3 aebstract

aebstract
  • Members
  • PipPipPip
  • Advanced Member
  • 1,105 posts

Posted 06 October 2006 - 03:01 AM

That code doesn't seem to do the trick either?  :-\

There is an area of the mind that could be called unsane, beyond sanity, and yet
not insane. Think of a circle with a fine split in it. At one end there's
insanity. You go around the circle to sanity, and on the other end of the
circle, close to insanity, but not insanity, is unsanity.


#4 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 06 October 2006 - 05:14 AM

i assumed that you knew about how to access variables when passing by URL.  change the $page to $_GET['page'] in case register_globals is not on.  read up on register_globals.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users