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
https://forums.phpfreaks.com/topic/23141-simple-elseif-problem/
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
https://forums.phpfreaks.com/topic/23141-simple-elseif-problem/#findComment-104778
Share on other sites

Archived

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

×
×
  • 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.