How can I define pages in an array? - php

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 ':''; ?>"><i class="fa fa-circle-o"></i> <?php echo $vbphrase['post_new_announcement'] ?></li>
<li class="<?= ($_GET['do'] == 'modify') ? 'active ':''; ?>"><i class="fa fa-circle-o"></i> <?php echo $vbphrase['forum_manager'] ?></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">

Related

Setting my active class in my navigation with PHP

Hello I would like to know how to use PHP in my header so that my class active can be activated when it is on the correct page.
for instance on my index.php
i have this at the top
<?php
$page = 'Home';
include("header.php");
?>
then this is in my navigation
<nav>
<ul>
<li><a class="active" href="index">Home</a></li>
<li class="rightside">Projects</li>
<li class="rightside">About</li>
<li class="rightside">Blog
<li class="rightside">Contact
</ul>
</nav>
I want the class to be activated when i am on the appropriate page if that makes any sense. Thank you, my PhP knowledge is minimal.
This is a shorthand if statement that can be used inline
<?php echo ($page == 'Home' ? 'active':'');?>
See Below
<nav>
<ul>
<li><a class="<?php echo ($page == 'Home' ? 'active':'');?>" href="index">Home</a></li>
<li class="rightside"><a class="<?php echo ($page == 'Projects' ? 'active':'');?>" href="projects">Projects</a></li>
<li class="rightside"><a class="<?php echo ($page == 'About_us' ? 'active':'');?>" href="about_us">About</a></li>
<li class="rightside"><a class="<?php echo ($page == 'Blog' ? 'active':'');?>" href="blog">Blog</a>
<li class="rightside"><a class="<?php echo ($page == 'Contact' ? 'active':'');?>" href="contact">Contact</a>
</ul>
</nav>
I personally use a jquery function for this, it saves having to declare the page type each time
jQuery
$(function () {
var url = window.location.href.substr(window.location.href.lastIndexOf("/") + 1);
$('[href$="'+url+'"]').parent().addClass("active");
});
If you always declare $page before the header include, than you can use an if statement.
Just an example:
<nav>
<ul>
<li><a class="<?php echo ($page == 'Home') ? 'active' : 'rightside'; ?>" href="index">Home</a></li>
<!-- same for your other list elements -->
</ul>
</nav>

PHP: How to securly set active link on page using includes

in an earlier post "PHP set active link on page using includes" there was a comment "That is waaaay too hackable". can you explain to me how it is hackable and how i can duplicate this idea with a safer method.
$page_id = $_REQUEST['page_id'];
<ul>
<li class="<?php echo ($page_id == "Home" ? "active" : "");?>">
Home
</li>
<li class="<?php echo ($page_id == "Apps" ? "active" : "");?>">
Apps
</li>
<li class="<?php echo ($page_id == "Forums" ? "active" : ""); ?>">
Forums
</li>
</ul>
I am building my first plugin for a website and i need to have links that php sets to active if you are on the page such as home or statistics. so the installation may be at www.website.com/home/plugin/statistics. I need to be able to set the class statistics to active if they are on the page without knowing how many layers deep in their website my plugin is installed.
A better approach may be something like this:
In the page files (about.php, index.php, etc.) provide the unique identifier at the very top of the page:
<?php
$page_id = 'home';
require_once('_menu.php');
Separate your navigation into a file _menu.php to be included.
<ul>
<li class="<?php echo ($page_id == "Home" ? "active" : "");?>">
Home
</li>
<li class="<?php echo ($page_id == "Apps" ? "active" : "");?>">
Apps
</li>
<li class="<?php echo ($page_id == "Forums" ? "active" : ""); ?>">
Forums
</li>
</ul>

PHP Print Conditional with another Print

I am new to PHP so forgive me if this is not the best way to achieve this goal. I am creating a sub navigation page and I wish to detect the parent variable and then render out the correct sub navigation for that parent. Then within that detect the child page the user is on and print an .active class to the sub navigation.
<?php print (($primaryNav == 'training') ? '
<li class="'print (($secondaryNav == 'classes') ? 'active' : '')'">
Classes
</li>
<li class="'print (($secondaryNav == 'calendar') ? 'active' : '')'">
Calendar
</li>
<li class="'print (($secondaryNav == 'instructors') ? 'active' : '')'">
Instructors
</li>
' : ''); ?>
Thank you for your help.
For the sake of being able to read the code I would do something like this instead:
<?php
if ($primaryNav == 'training') {
$classes = ($secondaryNav == 'classes') ? 'active' : '';
$calendar = ($secondaryNav == 'calendar') ? 'active' : '';
$instructors = ($secondaryNav == 'instructors') ? 'active' : '';
?>
<li class="<?php echo $classes; ?>">
Classes
</li>
<li class="<?php echo $calendar; ?>">
Calendar
</li>
<li class="<?php echo $instructors; ?>">
Instructors
</li>
<?php
}
?>

Using a querystring to highlight a menu item

So I'm trying to use a query string to highlight a 'current' menu item.
Say the url is www.....something.php?tag=Music
And I'm looping through this code to check the $tag against a record in the database:
<li class="<?php if(isset($_GET['tag']) && $_GET['tag'] == $record->name);
{ echo 'current'; }?>">
<a href="?tag=<?php echo $record->name; ?>">
<?php echo $record->name; ?></a></li>
Why doe's it always come out 'true' and echo 'current'.
The html it outputs is this:
<li class="current">
<a href="?tag=Music">Music</a>
</li>
<li class="current">
<a href="?tag=Film">Film</a>
</li>
<li class="current">
<a href="?tag=biscuits">biscuits</a>
</li>
Surely it should only be 'true' for 'Music'?
You have a semi-colon after your if statement. Remove that and it should work:
<li class="<?php if(isset($_GET['tag']) && $_GET['tag'] == $record->name)
For shorter code, and if you have short tags enabled, try:
<li class="<?=isset($_GET['tag'])&&$_GET['tag']==$record->name?'current':''?>">

PHP set active link on page using includes

So I have searched SO for an hour trying to find the answer, and also tried various methods, including this
I am trying to include my pages, along with the navigation. But on the correct page, I need the list-item to have a class of active. The navigation is in header.php and currently looks like this:
<nav>
<ul>
<li class="active"> Home </li>
<li> Apps </li>
<li> Forums </li>
</ul>
</nav>
First, I have no idea if JS(jQuery) would be better, or if PHP was to be better. Both wouldn't matter if it works.
Also I am a bit new with PHP and trying to learn.
What would be the easiest (hopefully) method to use? So I don't have to change a lot of code just to give a nav class="active"
Asumming you have a $page variable (which contains the name of the page you are currently on):
<nav>
<ul>
<li class="<?php echo ($page == "home" ? "active" : "")?>"> Home </li>
<li class="<?php echo ($page == "apps" ? "active" : "")?>"> Apps </li>
<li class="<?php echo ($page == "forums" ? "active" : "")?>"> Forums </li>
</ul>
</nav>
Here is a simple way where you do not need to add any other variable
<li class="<?php echo ($_SERVER['PHP_SELF'] == "/index.php" ? "active" : "");?>">
Start
</li>
<li class="<?php echo ($_SERVER['PHP_SELF'] == "/about.php" ? "active" : "");?>">
About
</li>
<li class="<?php echo ($_SERVER['PHP_SELF'] == "/practices.php" ? "active" : "");?>">
Practices
</li>
Add basename function before $_SERVER. I hope it will work.
echo (basename($_SERVER['PHP_SELF']) == 'yourPageName' ?'active' : " ");
At page header use:
<?php $loc = this.location; ?>
Then at every link add:
<?php(this.href == $loc) ? echo 'class="active"' : '' ?>
define variable $page="index.php" in index.php page and for other pages change the variable value according to the page name
<li class="<?php echo ($page == "index.php" ? "active" : "")?>">
Home
</li>
<li class="<?php echo ($page == "about.php" ? "active" : "")?>">
About
</li>
<li class="<?php echo ($page == "service.php" ? "active" : "")?>">
Services
</li>
<li class="<?php echo ($page == "contact.php" ? "active" : "")?>">
Contact
</li>
$page_url = $_SERVER['QUERY_STRING'];
$s = explode("&",$page_url);
//print $s[0];
$page = $s[0];
function createTopNav($page)
{
$pages = array(
array(
'name'=>'Feeder',
'link'=>'page=feeder'
),
array(
'name'=>'Services',
'link'=>'page=services'
),
array(
'name'=>'Development',
'link'=>'page=development'
),
array(
'name'=>'Design',
'link'=>'page=design'
),
);
$res = "";
foreach($pages as $key=>$val)
{
if($val['link']==$page)
{
$res.= "<a class=\"active\" href=\"?"
.$val['link'].
"\">"
.$val['name'].
"</a>";
}
else
{
$res.= "<a class=\"\" href=\"?"
.$val['link'].
"\" >"
.$val['name'].
"</a>";
}
}
$res.="";
return $res;
}
echo createTopNav($page);
if you are using query string exmaple.com?page_id=Apps to pass page id than with php you can
approach this thing
$page_id = $_REQUEST['page_id'];
<ul>
<li class="<?php echo ($page_id == "Home" ? "active" : "");?>">
Home
</li>
<li class="<?php echo ($page_id == "Apps" ? "active" : "");?>">
Apps
</li>
<li class="<?php echo ($page_id == "Forums" ? "active" : ""); ?>">
Forums
</li>
</ul>
If you don't want to use jQuery or PHP - can do next:
Give ID or different CLASS to each <li> element in "your-include-nav.php".
Define the "active" style with CSS in <head> section, on each page.
Add basename function. Then it will work i hope.

Categories