Jump to content

Archived

This topic is now archived and is closed to further replies.

aebstract

simple elseif problem

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]

Share this post


Link to post
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.

Share this post


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

Share this post


Link to post
Share on other sites

×

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.