1

I'm trying to check if the current URL equals the desired URL for making the link class active (Bootstrap).

Example

<?php
    If ($currenturl = "show.php?name=123") {
      ?><li class="active"><a href="show.php?name="123"">123</a></li><?php
    }else{
        ?><li class=""><a href="show.php?name="123"">123</a></li><?php
    }
?>

Current URL -> $currenturl = $_SERVER['REQUEST_URI']

Now, I have this if I'm on show.php?name=456

see

1
  • basename($_SERVER['PHP_SELF']); Commented Dec 8, 2014 at 0:57

2 Answers 2

4

Make sure you have the correct comparison inside the if. You're currently using the assignment operator:

Note the difference:

if($currenturl = "show.php?name=123") // assignment =
  // changes $currenturl to "show.php?name=123"
  // and then tests if("show.php?name=123") equivalent to if(true)
  // see also http://stackoverflow.com/questions/5940626/evaluation-of-assignment-in-php

if($currenturl == "show.php?name=123") // comparison ==

Second: You have set $urlpage but comparing $currenturl. Use $urlpage

Code:

<?php $urlpage = $_SERVER['REQUEST_URI']; ?>
<?php if ($urlpage == "/show.php?nom=123") { ?>
    <li class="active"><a href="/show.php?nom=123">123</a></li>
<?php } else { ?>
    <li class=""><a href="/show.php?nom=123">123</a></li>
<?php } ?>

An alternative using a ternary operator:

<li <?php echo ($urlpage == "/show.php?nom=123") ? 'class="active"' : ''; ?>><a href="/show.php?nom=123">123</a></li>

Applying to all pages:

<?php $pages = array('123', '456', '789'); ?>
<ul>
    <li <?php echo (!isset($_GET['nom']) ? 'class="active"' : ''); ?>><a href="show.php">Home</a></li>
    <?php foreach($pages as $page): ?>
        <?php if (isset($_GET['nom']) && $_GET['nom'] == $page) { ?>
            <li class="active"><a href="/show.php?nom=<?php echo $page; ?>"><?php echo $page; ?></a></li>
        <?php } else { ?>
            <li class=""><a href="/show.php?nom=<?php echo $page; ?>"><?php echo $page; ?></a></li>
        <?php } ?>
    <?php endforeach; ?>
</ul>
Sign up to request clarification or add additional context in comments.

6 Comments

@dotpush its okay to add supplemental info, no need to remove the ternary way as this was an alternative and part of the answer also
It's not working... It's just not actived! Also, when I click on the link, it's goes to show.php?name= but with no 123 or other.
@Ghost: Oups, sorry. I didn't even saw for the ternary way part removal.
@dotpush no prob, i have it back its okay
@InxDev i made revision, you can check it again,
|
0

An alternate way is to use the $_GET

if(isset($_GET['name']))
 if($_GET['name'] == '123') 
  ...

and so forth

4 Comments

Yes, i change to If ($_GET['name'] == '123') but I get an error : Notice: Undefined index: name in C:\xampp\htdocs\cadeau noel\cadeau.php on line 40. Line 40 -> If ($_GET['name'] == '123') {
What does URL look like? (In your browser)
Also thats why I suggested to put if(isset($_GET['name'])) if your URL does not have the ?name=123 or name=124, name=125 etc at the end of the URL, it will just give a warning. So either earlier in the code add if(!isset($_GET['name'])) $_GET['name'] = '';
Tank you, but I used the Ghost answer, it was better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.