Jump to content

[SOLVED] if statement in url (sorta)


aebstract

Recommended Posts

Hello,

I have done something just like what I wana do, I just can't get it to work right now. I want it to run an if statement to see if you are on that page or not and if you are then it will set that link's class to active. If not, doesn't do anything. Here is what I had to do in my previous site, and I can't get it to work on the new one, could someone assist me?

 

Previous working code:

echo '
<ul id="navlist">
	<li'.(($page == '1' || empty($page)) ? ' class="active"' : '').'><a href="/1/">Uniquely Yours</a></li>
	<li'.(($page == '2') ? ' class="active"' : '').'><a href="/2/">HAM</a></li>
	<li'.(($page == '3') ? ' class="active"' : '').'><a href="/3/">MacConnell Motors</a></li>
	<li'.(($page == '4') ? ' class="active"' : '').'><a href="/4/">Wilder Embry</a></li>
	<li'.(($page == '5') ? ' class="active"' : '').'><a href="/5/">d.terrell</a></li>
	<li'.(($page == '6') ? ' class="active"' : '').'><a href="/6/">Last November</a></li>
</ul>
';

 

 

Current links, any help appreciated! (just get me started on one link and ill do the rest)

echo '<div id=nav>
<a href=index.php>HOME</a>
<a href=index.php?page=bios>BIOS</a>
<a href=index.php?page=platform>PLATFORM</a>
<a href=index.php?page=events>EVENTS</a>
<a href=index.php?page=team>TEAM</a>

</div>';

 

 

The css and everything is fine, just needing to get the class=active to work in the links if possible. Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/
Share on other sites

It's been too long for me to edit my last post, but I thought I should post my current code:

 

<?php
echo '<div id=nav>';

$links = array ("home", "bios", "platform", "events", "team");
foreach ($links as $link) {
echo '<a href="index.php?page='.$link.'" '.(($page == $link) ? 'class="active"' : '').'>'.$link.'</a>
';
}

echo '</div>';
?>


Okay then, what is wrong with:

 

<?php
echo '<div id=nav>';

$links = array ("home", "bios", "platform", "events", "team");
foreach ($links as $link) {
echo '<a href="index.php?page='.$link.'" '.(($page == $link) ? 'class="active"' : '').'>'.$link.'</a>
';
}


echo '</div>';

?>

?

It's not setting the active link's class as active. Whichever page you are on, that link should be class=active so the css steps in with that link.

 

edit: here is how it is generating;

<div id=nav><a href="index.php?page=home" >home</a>

<a href="index.php?page=bios" >bios</a>
<a href="index.php?page=platform" >platform</a>
<a href="index.php?page=events" >events</a>
<a href="index.php?page=team" >team</a>
</div>

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.