简体   繁体   中英

How can I define pages in an array?

This seems so basic, yet I am struggling with it so bad. I am creating a navigation that has multi layer dropdowns using ul 's and li 's so in order to have each (the dropdown and active link within the dropdown) active I have to give classes to multiple elements. I go about this like so..

I defined a variable to detect the page name:

$activePage = basename($_SERVER['PHP_SELF'], ".php");

Now this covered MOST of the main drop downs, but a lot of the links are in requests ?do=page so to highlight each link I used:

<?= ($_GET['do'] == 'page') ? 'active ':''; ?>

An example of on of my dropdowns looks like:

<li class="<?= ($activePage == 'announcement' OR $activePage == 'forum') ? 'active ':''; ?>treeview">
  <a href="#">
    <i class="fa fa-bullhorn"></i> <span><?php echo $vbphrase['announcements'] ?></span>
    <span class="pull-right-container">
      <i class="fa fa-angle-left pull-right"></i>
    </span>
  </a>
  <ul class="treeview-menu">
    <li class="<?= ($_GET['do'] == 'add') ? 'active ':''; ?>"><a href="announcement.php?do=add"><i class="fa fa-circle-o"></i> <?php echo $vbphrase['post_new_announcement'] ?></a></li>
    <li class="<?= ($_GET['do'] == 'modify') ? 'active ':''; ?>"><a href="forum.php?do=modify"><i class="fa fa-circle-o"></i> <?php echo $vbphrase['forum_manager'] ?></a></li>
  </ul>
</li>

Which in it's self works, but some of my links and "categories" in different sections have the same $activePage

So I have been trying to make my category sections operate in the following mannor:

if in either of these pages AND any of these gets, be active

To do this I have been trying to create an array rather than having multiple OR... OR... OR... statements. This would all talk place on the first line of my code block, IE:

<li class="<?= ($activePage == array('announcement','forum') AND $_GET['do'] == array('add','modify')) ? 'active ':''; ?>treeview">

However I have not been successful at this. I have attempted creating array variables, i have tried arrays inline, it just does not seem to want to work.

How can I achieve getting an array for the desired outcome to prevent multiple conditions?

Have you tried this?

<?php 

$page_array = array('announcement','forum');
$action_array = array('add','modify');

?>

<li class="<?= (in_array($activePage, $page_array) AND in_array($_GET['do'], $action_array)) ? 'active ':''; ?>treeview">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM