Jump to content

Recommended Posts

I am using this code for a menu bar so that the "selected" id can be used by CSS for styling purposes.  I can't figure out why this code won't work...

 

<ul> 

<?

$page = $_SERVER['SCRIPT_NAME'];

?>

          <? if ($page == '/index.php') { ?>

              <li><a href="/index.php" class="selected">Home</a></li>

              <li><a href="/contact.php">Contact Us</a></li>

              <li><a href="/agents.php">Meet our Agents</a></li>

              <li><a href="/claims.php">Submit a Claim</a></li>

 

              <? }

              else if ($page == '/contact.php') { ?>

              <li><a href="http://www.dallmaninsurance.com">Home</a></li>

              <li><a href="contact.php" class="selected">Contact Us</a></li>

              <li><a href="agents.php">Meet our Agents</a></li>

              <li><a href="claims.php">Submit a Claim</a></li>

             

              <? } else if ($page == '/agents.php') { ?>

              <li><a href="http://www.dallmaninsurance.com">Home</a></li>

              <li><a href="contact.php">Contact Us</a></li>

              <li><a href="agents.php" class="selected">Meet our Agents</a></li>

              <li><a href="claims.php">Submit a Claim</a></li>

 

              <? } else if ($page == '/claims.php') { ?>

              <li><a href="http://www.dallmaninsurance.com">Home</a></li>

              <li><a href="contact.php">Contact Us</a></li>

              <li><a href="agents.php">Meet our Agents</a></li>

              <li><a href="claims.php" class="selected">Submit a Claim</a></li>

             

              <? } else { ?>

              <li><a href="http://www.dallmaninsurance.com">Home</a></li>

              <li><a href="contact.php">Contact Us</a></li>

              <li><a href="agents.php">Meet our Agents</a></li>

              <li><a href="claims.php">Submit a Claim</a></li>

              <? } ?>

          </ul>

 

On the web, it gets all the way to the last "else" and outputs those list items.  In wampserver, it outputs all of the list items (so there are 5 menu bars).

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/184685-coding-help/
Share on other sites

You may have to use stristr to check the file:

 

           <?php if (stristr($page, '/index.php')) { ?>
               <li><a href="/index.php" class="selected">Home</a></li>
               <li><a href="/contact.php">Contact Us</a></li>
               <li><a href="/agents.php">Meet our Agents</a></li>
               <li><a href="/claims.php">Submit a Claim</a></li>

               <?php }

 

I would also convert your <? to <?php for compatibility reasons, as short_tags have been defaulted to off in php 4.x I believe. But see if that works for you.

Link to comment
https://forums.phpfreaks.com/topic/184685-coding-help/#findComment-975049
Share on other sites

Thank you!  Premiso's suggestion worked. 

 

I'm using the code in an includes file, so I'm not sure that declaring $page on each site would work for me.  I'm so new to this, though, that I could be completely wrong.  Thank you so much for saving me a lot of time!

 

How do I mark this problem as solved on this site (1st day on phpfreaks)?

Link to comment
https://forums.phpfreaks.com/topic/184685-coding-help/#findComment-975078
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.