PHP Print Conditional with another Print - php

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
}
?>

Related

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 ':''; ?>"><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">

How can I show css active class using php?

Here is my site nav.php is look like :
<li <?php echo $active; ?>>Home</li>
<li <?php echo $active; ?>>Report</li>
<li <?php echo $active; ?>>Chart</li>
<li <?php echo $active; ?>>Export</li>
and header.php page is look like :
<?php
$pageName = basename($_SERVER['PHP_SELF']);
$explode = explode('.', $pageName);
$title = ucfirst($explode[0]);
if($title == "Report") {
$active = 'class="active"';
} else {
$active = '';
}
?>
I am using above code to show css .active class on currently view any page. but it's adding .active class to all pages.
Note : I have 4 pages. e.g: index, report, chart and export.
and My full page structure is like that :
In index.php page I am calling following page.
header.php
nav.php
js.php
footer.php
use this
$activePage = basename($_SERVER['PHP_SELF'], ".php");
and in your li
<li <?php if($activePage=="Report") { echo "class='active'"; } ?>>Home</li></li>
Wrapping will give you additional flexibility over static content
$pageName = basename($_SERVER['PHP_SELF']);
$explode = explode('.', $pageName);
$title = ucfirst($explode[0]);
$nav = [
'index' => 'Home',
'report' => 'Report',
'chart' => 'Chart',
'export' => 'Export'
];
foreach ($nav as $key => $value) {
echo '<li class="'.($key == $title ? 'active' : '').'>';
echo ''.$value.'' ;
echo '</li>'
}
Easiest way:
$pages = array();
$pages["home.php"] = "Home";
$pages["report.php"] = "Report";
$pages["chart.php"] = "Chart";
$pages["export.php"] = "Export";
$active = "home.php";
<?php foreach($pages as $url=>$title):?>
<li>
<a <?php if($url === $active):?>class="active"<?php endif;?> href="<?php echo $url;?>">
<?php echo $title;?>
</a>
</li>
<?php endforeach; ?>
Because you're defining $active only once and for all at the same time.
You probably should work with an array:
$navigation = array('index', 'report', 'chart', 'export);
And now you loop through that array, checking, which is active:
foreach($navigation as $n) {
if(ucfirst($explode('.', $pageName) == $n) {
echo '<li class="active">' . ucfirst($n) . '</li>';
} else {
echo '<li>' . ucfirst($n) . '</li>';
}
}
Try to do it like this:
<li class="<?= ('Home' == $title) ? 'active' : ''; ?>">
Home
</li>
<li class="<?= ('Report' == $title) ? 'active' : ''; ?>">
Report
</li>
<li class="<?= ('Chart' == $title) ? 'active' : ''; ?>">
Chart
</li>
<li class="<?= ('Export' == $title) ? 'active' : ''; ?>">
Export
</li>
try doing the following
<li <?php echo ($pageName=="index.php")?" class='active'" :""; ?>>Home</li>
<li <?php echo ($pageName=="report.php")?" class='active'" :""; ?>>Report</li>
<li <?php echo ($pageName=="chart.php")?" class='active'" :""; ?>>Chart</li>
<li <?php echo ($pageName=="export.php")?" class='active'" :""; ?>>Export</li>

Toggle bootstrap pills with php

Can I toggle bootstrap pills when a variable is set on my url using php?
example
if (isset($_GET['user'])){
#profile is active
}
else {
#home is active
}
Sure, you just have to add a class="active" depending on your conditions. I would do it like this:
<ul class="nav nav-pills">
<?php
if (isset($_GET['user'])) {
$activeMenu = 'profile';
}
else {
$activeMenu = 'home';
}
?>
<li role="presentation" <?php echo ($activeMenu == 'home' ? 'class="active"' : ''); ?>>Home</li>
<li role="presentation" <?php echo ($activeMenu == 'profile' ? 'class="active"' : ''); ?>>Profile</li>
<li role="presentation">Messages</li>
</ul>
So it sets the $activeMenu variable to the menu you want active. And then we check that variable in each menu item and add the "active" class depending on its value.
Note this doesn't activate other menus (Messages) so you would have to add another condition (like I have) if you have other pills that you want active.

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