Make tab active in Bootstrap - php

I want to make a second tab active in Bootstrap, when i set the class="active" on the second li it shows active but shows the content of the first tab. I'm using PHP to set active tab, with two variables $login_tab and $signup_tab
$login_tab = 'class="active"';
$signup_tab = '';
if (isset($_GET['joined']) && $_GET['joined'] === 'true'){
$login_tab = '';
$signup_tab = 'class="active"';
}
and my list
<li role="presentation" <?php echo $login_tab; ?>>Login</li>
<li role="presentation" <?php echo $signup_tab; ?>>Sign-up</li>
I have tried with jQuery and still doesn't work.
$('#sign-up-tabs li.active').tab('show');
How can I make a tab active based on the values of the two variables?

You also must add the class active to the tabpanel inside the tab-content.
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="user-sign-in">...</div>
<div role="tabpanel" class="tab-pane" id="user-sign-up">...</div>
</div>

Related

dynamic tabs inside Bootstrap 5 card

hope you're doing good !
I'm working on the interface of my activity report based on an SQL database and illustrated with pies created with a JS library "Charts".
So I want to display the different pies (depending of the year) in one bootstrap card with a nav where clickable years will be displayed. Just like the exemple here https://www.codeply.com/p/t6GGNuV2yb
To display the different nav buttons and the pies I'm using foreach php loops ($dataphp is the dictionnary structure that the js chart library requiers). The "active" class should handle the displaying tabs on nav items click but in my case, it doesn't work. In my code I add the class to the current year and this is the result :
https://i.stack.imgur.com/1YmPf.png
Thanks to everyone proposing a solution.
Here is my code :
<div class="card">
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs" data-bs-tabs="tabs">
<?php foreach($dataphp as $annee => $value){
echo '<li class="nav-item">
<a class="nav-link '; if($annee==2022) echo 'active'; echo ' " href="#chartdiv'.$annee.'"><strong>'.$annee.'</strong></a>
</li>';
}; ?>
</ul>
</div>
<form class="card-body tab-content graph">
<?php foreach($dataphp as $annee => $value){
echo '<div class="tab-pane '; if($annee==2022) echo 'active'; echo ' graph" id="chartdiv'.$annee.'"></div>';
}; ?>
</form>
</div>

Highlight current item in the heading php

So I was wondering how do you highlight the current page in the heading of the website. I used the include method to call the heading in all of the pages, but how do I highlight the current page while maintaining the include method in every page.
HEADER.PHP
<div id="site-content">
<header class="site-header">
<div class="container">
<a href="index.php" id="branding">
<img src="images/logo.png" alt="Company Name" class="logo" width="100">
<div class="branding-copy">
<h1 class="site-title" style="font-size:60px;">Benedicto Law</h1>
<small class="site-description">Justice In Words</small>
</div>
</a>
<nav class="main-navigation">
<button type="button" class="menu-toggle"><i class="fa fa-bars"></i></button>
<ul class="menu">
<li class="menu-item">Home</li>
<li class="menu-item">Attorney</li>
<li class="menu-item">Service</li>
<li class="menu-item">Contact</li>
</ul>
</nav>
<nav class="mobile-navigation"></nav>
</div>
</header> <!-- .site-header -->
then I only used
<?php include_once('header.php') ?> for calling it to other pages.
I wanted it to highlight the current menu item where the user is in.
For example:
The user pressed Attorney button the Attorney button in the heading should have a highlight.
Any help is appreciated thanks
You have at least 2 options.
1. Pass the data in PHP manually
Before your include(header statement, create a variable for the onPage and set it. Then use it in your include.
For example:
<?php
$onPage = 'attorney';
include_once('header.php') ?>
Then in header.php, check for it like this:
<li class="menu-item <?php if ($onPage == 'attorney') echo 'active'; ?>">Attorney</li>
2. Automatically detect it
In header.php
$onPage = $_SERVER['PHP_SELF'];
// make sure it's getting just the page, no directory
$onPage= explode("/",$onPage);
$onPage = $onPage[count($onPage-1)];
// remove .php and make sure its lower case
$onPage = str_replace(".php", "", strtolower($onPage));
// for /directory/Attorney.php this will give you 'attorney'
The check for it like before
<li class="menu-item <?php if ($onPage == 'attorney') echo 'active'; ?>">Attorney</li>

Highlight link based on current page (Dynamic Navigation PHP)

This might be very easy for some but I've tried various ways to get it to work but to no avail.
Here's a brief;
I have one page, I have links in this page that when clicked load other pages but display them in the same page.
<section class="posts-header">
<div class="col span-3-of-3">
<ul class="posts-nav" id="navigation">
<li>View Posts</li>
<li>View Posts</li>
<li>Create Post</li>
</ul>
</div>
</section>
The above code shows links that when clicked, pass a variable using $_GET to the switch statement below;
if(isset($_GET['source'])) {
$source = $_GET['source'];}
switch($source) {
case 'view_posts';
include "php/blog_posts_view.php";
break;
case 'view_all_posts';
include "php/blog_posts_reviewer.php";
break;
case 'add_posts';
include "php/blog_posts_addform.php";
break;
case 'edit_posts';
include "php/blog_posts_editform.php";
break;
default:
include "php/blog_posts_view.php";
break;}
How can I highlight the current page? using html, js, css3 or all.
Thanks.
You can just define an active class in CSS, which describes the style of an active link. For example, a very simple style:
.active {
color: red;
}
Then you have to modify your menu:
<section class="posts-header">
<div class="col span-3-of-3">
<ul class="posts-nav" id="navigation">
<li><a href="blog_admin_posts.php?source=view_all_posts"<?=$_GET['source'] == "view_all_posts" ? " class=\"active\"" : ""; ?>>View Posts</a></li>
<li><a href="blog_admin_posts.php?source=view_posts"<?=$_GET['source'] == "view_posts" ? " class=\"active\"" : ""; ?>>View Posts</a></li>
<li><a href="blog_admin_posts.php?source=add_posts"<?=$_GET['source'] == "add_posts" ? " class=\"active\"" : ""; ?>>Create Post</a></li>
</ul>
</div>
</section>
The active menu link text should now be displayed in red.
maybe you will find this very helpful, it adds your class to the current page and allows to add more links to your page by simply adding to the array.
<?php
$page = isset($_GET['page']) ? $_GET['page']: ' ';
$pages = ['view_all_posts','view_posts','add_posts'];
$active = '';
if (in_array($page,$pages)){
$active = 'active';
}
?>
Then modify your menu:
<section class="posts-header">
<div class="col span-3-of-3">
<ul class="posts-nav" id="navigation">
<?php foreach ($pages as $key => $value) : ?>
<li>
<?=$value?>
</li>
<?php endforeach;?>
</ul>
</div>
</section>
You can get the current page name using this:
$pageName = $_GET["source"];
And then, for every list item, you could do something like this:
<li class="<?= ($pageName === 'firstPageName') ? 'active' : '' ?>">...</li>
Of course, you'll need to style the class .active or how ever you would like to name it.

How to set current page "active" in php

Hi I have a menu on my site on each page, I want to put it in it's own menu.php file but i'm not sure how to set the class="active" for whatever page i'm on.
Here is my code: please help me
menu.php:
<li class=" has-sub">
<a class="" href="javascript:;"><i class=" icon-time"></i> Zeiten<span class="arrow"></span></a>
<ul class="sub">
<li><a class="" href="offnungszeiten.php">Öffnungszeiten</a></li>
<li><a class="" href="sauna.php">Sauna</a></li>
<li><a class="" href="frauensauna.php">Frauensauna</a></li>
<li class=""><a class="" href="custom.php">Beauty Lounge</a></li>
<li><a class="" href="feiertage.php">Feiertage</a></li>
</ul>
</li>
this method gets the current page using php which will pass a word in this case active and places it inside the class parameter to set the page active.
<?php
function active($currect_page){
$url_array = explode('/', $_SERVER['REQUEST_URI']) ;
$url = end($url_array);
if($currect_page == $url){
echo 'active'; //class name in css
}
}
?>
<ul>
<li><a class="<?php active('page1.php');?>" href="http://localhost/page1.php">page1</a></li>
<li><a class="<?php active('page2.php');?>" href="http://localhost/page2.php">page2</a></li>
<li><a class="<?php active('page3.php');?>" href="http://localhost/page3.php">page3</a></li>
<li><a class="<?php active('page4.php');?>" href="http://localhost/page4.php">page4</a></li>
</ul>
It would be easier if you would build an array of pages in your script and passed it to the view file along with the currently active page:
//index.php or controller
$pages = array();
$pages["offnungszeiten.php"] = "Öffnungszeiten";
$pages["sauna.php"] = "Sauna";
$pages["frauensauna.php"] = "Frauensauna";
$pages["custom.php"] = "Beauty Lounge";
$pages["feiertage.php"] = "Feiertage";
$activePage = "offnungszeiten.php";
//menu.php
<?php foreach($pages as $url=>$title):?>
<li>
<a <?php if($url === $activePage):?>class="active"<?php endif;?> href="<?php echo $url;?>">
<?php echo $title;?>
</a>
</li>
<?php endforeach;?>
With a templating engine like Smarty your menu.php would look even nicer:
//menu.php
{foreach $pages as $url=>$title}
<li>
<a {if $url === $activePage}class="active"{/if} href="{$url}">
{$title}
</a>
</li>
{/foreach}
Create a variable in each of your php file like :
$activePage = "sauna"; (different for each page)
then check that variable in your html page like this
<?php if ($activePage =="sauna") {?>
class="active" <?php } ?>
Put all the below code in menu.php and everything will be taken care of.
// function to get the current page name
function PageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$current_page = PageName();
Use the above to get the current page name then put this in your menu
<li><a class="<?php echo $current_page == 'offnungszeiten.php' ? 'active':NULL ?>" href="offnungszeiten.php">Öffnungszeiten</a></li>
<li><a class="<?php echo $current_page == 'sauna.php' ? 'active':NULL ?>" href="sauna.php">Sauna</a></li>
<li><a class="<?php echo $current_page == 'frauensauna.php' ? 'active':NULL ?>" href="frauensauna.php">Frauensauna</a></li>
<li><a class="<?php echo $current_page == 'custom.php' ? 'active':NULL ?>" href="custom.php">Beauty Lounge</a></li>
<li><a class="<?php echo $current_page == 'feiertage.php' ? 'active':NULL ?>" href="feiertage.php">Feiertage</a></li>
where active is the name of the class which will highlight your menu item
there is two things you can do.
first you can read the current filename of the php file you request by using $_SERVER['PHP_SELF'] or $_SERVER['REQUEST_URI'] or any other $_SERVER global variables that you can use to read your current page and compare it with the link's url, something like this
<a href="offnungszeiten.php" <?php if($_SERVER['PHP_SELF']=='offnungszeiten.php'){ ?>class="activatepage" <?php } ?> >
Öffnungszeiten
</a>
the second one is to create a variable that you can read globally that would store the current name of the current page, like this
<?php
$cur_page ="offnungszeiten"
?>
<a href="offnungszeiten.php" <?php if($cur_page=='offnungszeiten'){ ?>class="activatepage" <?php } ?> >
Öffnungszeiten
</a>
I have done it with php in this way,
function createTopNav($active)
{
$pages = array(
array(
'name'=>'Home',
'link'=>'index'
),
array(
'name'=>'Smartphone',
'link'=>'smartphone'
),
array(
'name'=>'Tablet',
'link'=>'tablet'
),
array(
'name'=>'About Us',
'link'=>'about'
),
array(
'name'=>'Contact Us',
'link'=>'contact'
)
);
$res = "<ul>";
$activePage = "";
foreach($pages as $key=>$val)
{
if($val['link']==$active)
{
$res.= "<li><a href='".$val['link']."' class='active' >".$val['name']."</a></li>";
}
else
{
$res.= "<li><a href='".$val['link']."'>".$val['name']."</a></li>";
}
}
$res.="</ul>";
return $res;
}
And then to call this function
echo createTopNav("about");
and the output will be like this
<ul>
<li>Home</li>
<li>Smartphone</li>
<li>Tablet</li>
<li>About Us</li>
<li>Contact Us</li>
</ul>
I solved this using jQuery/javascript by running the code below each time my any page is loaded:
$(document).ready(function () {
//Get CurrentUrl variable by combining origin with pathname, this ensures that any url appendings (e.g. ?RecordId=100) are removed from the URL
var CurrentUrl = window.location.origin+window.location.pathname;
//Check which menu item is 'active' and adjust apply 'active' class so the item gets highlighted in the menu
//Loop over each <a> element of the NavMenu container
$('#NavMenu a').each(function(Key,Value)
{
//Check if the current url
if(Value['href'] === CurrentUrl)
{
//We have a match, add the 'active' class to the parent item (li element).
$(Value).parent().addClass('active');
}
});
});
This implementation assumes your menu has the 'NavMenu' ID, and uses http://hostname/scriptname.php href attributes like so:
<ul id="NavMenu">
<li>Home</li>
<li>Smartphone</li>
<li>Tablet</li>
<li>About Us</li>
<li>Contact Us</li>
</ul>
Read the javascript comments to see what's going on. If you prefer to use a different href layout (like in your original example), you have to play with the CurrentUrl variable a bit to get it to use the same layout as your href attributes.
For me this was the easiest solution since I had an existing sites with a big menu and many pages, and wanted to avoid having to modify all pages. This allows me to throw in a piece javascript code in the header file (which was a central file already) which solves the problem for all existing pages.
A bit late on the ball, but I just had to solve this myself and ended up using this Javascript method, with a small modification. This has the advantage on not requiring many changes to the current code, just run the script and voila.
window.onload = activateCurrentLink;
function activateCurrentLink(){
var a = document.getElementsByTagName("A");
for(var i=0;i<a.length;i++)
if(a[i].href == window.location.href.split("#")[0])
a[i].className = 'activelink';
}
Send page name in query string and check it on every page by getting the variable.
Simplere solution:
Borrowing the code from asprin above;
Create a new file menu.php where you will store the one and only copy of the menu. In this file, you will create a function addMenu($pageName) that take a parameter as the page name and returns a string consisting of the menu after having added the current tag.
In your HTML code, you would include(menu.php) and then call the function addMenu with the current page name. So your code will look like this:
menu.php
<?php
function addMenu($pageName){
$menu =
'<ul>
<li><a href="Öffnungszeiten.php"' . ($pageName == "Öffnungszeiten" ? "class=\"current\"" : "") . '><span>Öffnungszeiten</span></a></li>
<li><a href="sauna.php"' . ($pageName == "Öffnungszeiten" ? "class=\"current\"" : "") . '><span>Sauna</span></a></li>
<li><a href="frauensauna.php"' . ($pageName == "Frauensauna" ? "class=\"current\"" : "") . '><span>Frauensauna</span></a></li>
<li><a href="custom.php" ' . ($pageName == "lounge" ? "class=\"current\"" : "") . '><span>Beauty Lounge</span></a></li>
<li><a href="Feiertage.php"' . ($pageName == "feiertage" ? "class=\"current\"" : "") . '><span>Feiertage</span></a></li>
</ul>';
return $menu;
}
?>
And in your HTML, say this:
<div id="menu">
<?php
include('menu.php');
echo addMenu("index");
echo $hello;
?>
</div>
This worked for me:
function active_page($script){
$actual = basename($_SERVER['PHP_SELF']);
if($script == $actual){
return 'active-page'; //class name in css
}
}
I have some simple example, see below:
<?php
function active($currect_page) {
$url = $_SERVER['REQUEST_URI'];
if($currect_page == $url){
echo 'active';
}
}
?>
<ul class="navbar-nav mr-auto">
<li class="nav-item <?php active('/');?>">
<a class="nav-link" href="/">Home</a>
</li>
<li class="nav-item <?php active('/other');?>">
<a class="nav-link" href="/other">Other page</a>
</li>
</ul>
Better late than never - I like to keep it simple, to be honest, especially if there's a ton of scripting and PHP going on.
I place this code on the top of each page to identify the page:
<?php
$current_page = 'home';
include 'header.php';
?>
Then your menu/navigation (mine is bootstrap 4) looks like this:
<ul class="navbar-nav mx-auto">
<li class="nav-item <?php if ($current_page=="home") {echo "active"; }?>">
<a class="nav-link" href="<?php echo SITEURL;?>/">Home</a>
</li>
<li class="nav-item <?php if ($current_page=="about") {echo "active"; }?>">
About
</li>
<li class="nav-item <?php if ($current_page=="store") {echo "active"; }?>">
Store
</li>
<li class="nav-item <?php if ($current_page=="news") {echo "active"; }?>">
News
</li>
<li class="nav-item <?php if ($current_page=="contact") {echo "active"; }?>">
Contact
</li>
</ul>
I'm not saying this is the optimal method, but it works for me and it's simple to implement.
adding this:<?= ($activePage == 'home') ? 'active':''; ?> to my link it works perfectly, I only can't make the child of a submenu working to make the parent active.
Assume you have a navbar with the following items:
<ul>
<li id="menu-item-home">HOME</li>
<li id="menu-item-services">SERVICES</li>
<li id="menu-item-about-us">ABOUT US</li>
<li id="menu-item-contact">CONTACT</li>
</ul>
Then, declare a javascript variable in each page as below:
<script>
<?php echo("var active = 'menu-item-home';"); ?>
</script>
The variable "active" is assigned with the corresponding item of each page.
Now, you can use this variable to highlight the active menu item as below.
$(window).ready(function(){$("#" + active).addClass("active");});
I have a similar issue with my web app menu.
I also have sub menus which do not appear as top level menu buttons.
My solution is as follows:
a) Partial php file with menu html and a little php function at the top that checks GET variables against the menu buttons.
I have two GET variables to check: the page and (if necessary) the menu_button.
b) Adding any new php page with a href links to either menu pages or sub menu pages.
The variable "menu_button" is optional and can be used to link to submenu php files.
Of course the security concerning GET variables should be considered.
From my point of view, this solution has less effort than having to maintain an array of pages or links somewhere.
You just use a get variable "menu_button" where you pass the top level menu button that should be marked visually in any link which targets your php file.
Code examples:
Partial menu.php (has to be included in every php file):
<?php
function active($page_link){
$menu_button = $_GET("menu_button") ?: $_GET("page"); // sets the menu button either to the given top level menu or it defaults to the page itself
if($menu_button === $page_link) return "active";
}
?>
<div>
<a href="?page=one" class="<?= active('one') ?>"Link one</a>
Link two
</div>
Any php file with links to sub menu file:
<div>
Link one
Link to sub menu page "three" of menu "two"
</div>
Works for me. Hope someone else can use this.
For making a dynamic active menu link I follow this method.
first, In the menu link, I always use the full address:
//HTML CODE
<ul class="menu">
<li>
Home
</li>
<li>
About us
</li>
<li>
Contact
</li>
</ul>
//Javacript Code
const menus = document.querySelectorAll('.menu li a');
menus.forEach((menu) => {
const currentLocation = window.location.href;
if (currentLocation === window.origin) {
menus[0].classList.add('active');
} else if (menu.href === currentLocation) {
menu.classList.add('active');
} else {
return;
}
});
and then I will use vanilla javascript code to do the rest
You can use
<?php
function active($current_page){
$page = $_GET['p'];
if(isset($page) && $page == $current_page){
echo 'active'; //this is class name in css
}
}
?>
<ul>
<li><a class="<?php active('page1');?>" href="?p=page1">page1</a></li>
<li><a class="<?php active('page2');?>" href="?p=page2">page2</a></li>
<li><a class="<?php active('page3');?>" href="?p=page3">page3</a></li>
<li><a class="<?php active('page4');?>" href="?p=page4">page4</a></li>
</ul>

Can highlight the current menu item, but can't add the class= to style unhighleted menu items

<?php $activesidebar[$currentsidebar]="id=isactive";?>
<div class="span3">
<div class="well sidebar-nav hidden-phone">
<ul class="nav nav-list">
<li class="nav-header" <?php echo $activesidebar[1] ?>>Marketing Services</li>
<li>Marketing Technology</li>
<li>Generate More Sales</li>
<li>Direct Email Marketing</li>
<li class="nav-header" <?php echo $activesidebar[2] ?>>Advertising Services</li>
<li>Traditional Medias</li>
<li>Online & Social Medias</li>
<li>Media Planing & Purchasing</li>
<li class="nav-header" <?php echo $activesidebar[3] ?>>Technology Services</li>
<li>Managed Websites</li>
<li>Managed Web Servers</li>
<li>Managed Databases</li>
<li class="nav-header" <?php echo $activesidebar[4] ?>>About Us</li>
<li>Contact Us</li>
</ul>
</div>
This is added to the current page I want to add this on.
<?php $currentsidebar =2; include('module-sidebar-navigation.php');?>
I had programmed this menu individually on each page, but to make my website dynamic I used one file and use php includes to load the file. I can get the menu to highlight on the current page assigning an id="isactive", how can I assign id="notactive" to the other 3 menu items that are not active on that page. Is there an else or elseif I have to include?
Reset the array before setting your preferred index.
for ($i = 0; $i < 4; $i++) {
$activesidebar[$i] = "class=\"noactive\"";
}
Notes:
PHP array indices start from 0, not 1.
ID's are meant to be unique (ie, there cannot be 2 of the same ID). Use a classname instade.

Categories