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
Share on other sites

first of all it might be easier to do the following:

 

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

Link to comment
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>';
?>


Link to comment
Share on other sites

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>';

?>

?

Link to comment
Share on other sites

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>

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.