Show active navigation for current page with php

后端 未结 3 839
难免孤独
难免孤独 2021-01-06 09:46

I\'m trying to use use $current_url = basename($_SERVER[\'PHP_SELF\']); to determine which page I am on, considering my navigation (html) is stored in a php fil

相关标签:
3条回答
  • 2021-01-06 10:36

    You are using <?php } ?> at wrong place.
    It should be after
    <a href="search.php?Agent=<?php echo $Agent?>">Add new client</a></li>

    Updated code is as follows:

       <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/no-background.css">
    
    <ul>
    <?php
        $current_url = basename($_SERVER['PHP_SELF']);
        echo $current_url;
        $active = "class=\"active\"";
    ?>
    <?php if ($current_url == "globaluser.php") { ?>
        <li <?php echo $active;?> > <?php } ?> <a href="globaluser.php?Agent=<?php echo $Agent;?>">Overview</a></li>
    <?php if ($current_url == "search.php") { ?>
        <li <?php echo $active;?> > <a href="search.php?Agent=<?php echo $Agent?>">Add new client</a></li> <?php } ?> 
    <?php if ($current_url == "viewadmins.php") { ?>
        <li <?php echo $active;?> >  <a href="viewadmins.php?Agent=<?php echo $Agent?>">View admins</a></li><?php } ?>
        <li class="border-right"><a href="emails.php?Agent=<?php echo $Agent?>">E-mails</a></li>
        <li class="right border-right"><a href="logout.php"><?php echo $Agent?></a>
            <ul class="drop1">
    
                <li><a href="earnings.php?Agent=<?php echo $Agent?>">Earnings</a></li>
    
                <li id="hover-trigger"><a href="#">Change Password</a>
    
                    <ul class="drop2">
                        <li>
                            <form action="changepass.php?Agent=<?php echo $Agent?>" method="POST">
                                <input type="password" name="1" placeholder="Enter new password">
                                <input type="password" name="2" placeholder="Repeat new password">
                        </li>
                        <li>
    
                                <input type="submit" name="changepw" class="button" value="Change">
                            </form>
                        </li>
                    </ul>
                </li>
                <li>
                    <form action="logout.php">
                    <input type="submit" class="button" value="Log out">
                    </form>
                </li>
            </ul>
        </li>
    
    0 讨论(0)
  • 2021-01-06 10:40

    You should do it like this:

    <li <?php echo ($current_url == "globaluser.php") ? $active : ''?> ><a href="globaluser.php?Agent=<?php echo $Agent;?>">Overview</a></li>
    

    because right now you're conditioning the presence of the opening <li> tag

    0 讨论(0)
  • 2021-01-06 10:44

    You could also use preg_replace() to add class="active"

    ob_start();
    echo '<ul>
      <li><a href="page1.php">Page 1</a></li>
      <li><a href="page2.php">Page 2</a></li>
      </ul>';
    $output = ob_get_clean();
    $pattern = '~<li><a href="'.$url.'">~';
    $replacement = '<li class="active"><a href="'.$url.'">';
    echo preg_replace($pattern, $replacement, $output);
    
    0 讨论(0)
提交回复
热议问题