Jump to content

simple elseif problem


aebstract

Recommended Posts

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/

[code]<?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>';
}


?>[/code]
Link to comment
Share on other sites

much more effective ways of doing this:

[code]<?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>';
?>[/code]

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.