Toggle bootstrap pills with php - 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.

Related

.active class via PHP

I created this PHP function:
function active_class($page){
global $pagebase;
if($pagebase == $page){
return "active";
}
elseif($pagebase == null){
$page = "home";
return "active";
}
else{
return "";
}
}
But when I use it with this list:
<ul class="nav navbar-nav">
<li class="<?php echo active_class("home"); ?>">Home</li>
<li class="<?php echo active_class("about"); ?>">About</li>
</ul>
It shows the active class on both at http://example.com without the /home or /about. I am looking for a way to fix this. Any ideas?
$pagebase is just the $_GET['page']. It is the /home or /about for each page.
When $pagebase is null, for it to make just the home tab active, you have to add in - $page == "home" - condition so that the null state is just for the home tab and only returns active once.
Try:
function active_class($page){
global $pagebase;
if($pagebase == $page){
return "active";
}
elseif($pagebase == null && $page == "home" ){
$page = "home";
return "active";
}
else{
return "";
}
}
Another alternative:
<ul class="nav navbar-nav">
<li class="<?php echo ($pagebase=="home"?"active":""); ?>">Home</li>
<li class="<?php echo ($pagebase=="about"?"active":""); ?>">About</li>
</ul>
<li class="<?php echo ($pagebase == 'home')?'active':'';; ?>">Home</li>
I am sure it will work like this way.
After looking back at my code, I figured that I can just change $pagebase to equal home if it is null (No $_GET['page']. This would allow me to use my code meanwhile not using the elseif. Thank you for the replies.

How to have a navbar highlight the page you are in php

So, I am trying to make a personal web site. The site i am making has a navigation bar in the header section. In that navbar i have a few links Home Blog About Contact-Me. I want to make it so that it checks what page you are in and adds the class active to the link i am currently on. example I am on the home page and the Home link has the class active. when i click on about, about NOW has the class active and home doesn't.
Use This Code
.nav li.active a{
color: #de990a !important;
}
<ul class="nav">
<li <?php if (basename($_SERVER['PHP_SELF']) == 'home.php') echo 'class="active"' ?>>Home</li>
<li <?php if (basename($_SERVER['PHP_SELF']) == 'blog.php') echo 'class="active"' ?>>Blog</li>
<li <?php if (basename($_SERVER['PHP_SELF']) == 'about.php') echo 'class="active"' ?>>About</li>
<li <?php if (basename($_SERVER['PHP_SELF']) == 'contact.php') echo 'class="active"' ?>>Contact Me</li>
</ul>
add_filter('nav_menu_css_class' , 'active_nav_class' , 10 , 2);
function active_nav_class ($classes, $item) {
if (in_array('current-menu-item', $classes) ){
$classes[] = 'active ';
}
return $classes;
}
Active class will be added to the current nav item and you can add your css to the .active class.

PHP easier way to hide/show menu items to logged in / logged out users

Is there an easier more efficient way to hide/show menu items to logged in, logged out users? It seems like I should not have to copy the whole menu again with duplicate menu items. The menu items maybe in different order like below
You can see in my example below I have added to links in the <?php } else { ?> statement
<?php
if($_SESSION["loggedIn"] == "yes") {
?>
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Stores</li>
<li>Coupons</li>
<li>Featured Offers</li>
<li>How It Works</li>
<li>Help</li>
</ul>
<?php } else { ?>
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Stores</li>
<li>Coupons</li>
<li>My Account</li>//logged in item
<li>Featured Offers</li>
<li>How It Works</li>
<li>Help</li>
<li>My Favorite Stores</li>//logged in item
</ul>
<?php
}
?>
Updated for new ordering of items:
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Stores</li>
<li>Coupons</li>
<?php
if($_SESSION["loggedIn"] == "yes") {
echo '<li>My Account</li>';
}
?><li>Featured Offers</li>
<li>How It Works</li>
<li>Help</li>
<?php
if($_SESSION["loggedIn"] == "yes") {
echo '<li>My Favorite Stores</li>';
}
?>
</ul>
Rather than cluttering up the code, you can place the html into multiple PHP files that are loaded in dependant on the logged in user...
switch($_SESSION['userLevel']) {
case "guest": //Not logged in
require_once('guestnav.php');
break;
case "user": //regular user
require_once('usernav.php');
break;
case "admin": //admin nav
require_once('adminnav.php');
break;
//etc and default nav below
}
then in the nav php files just put the HTML with no PHP and it will be loaded in when the proper criteria is met. makes managing multiple navigation menu's easy.

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