Class active when on page using php - php

I have an include file that includes the header and footer of my site. But in the header I use an active class to let the menu-item change when on that page. How can you achieve this using one include file? With PHP?
I found this:
index.php
<?php $page == 'one'; include('includes/header.php'); ?>
header.php
<li>
<a <?php echo ($page == 'one') ? "class='active'" : ""; ?>
href="contact.php">Contact</a>/</li>
But it's not changing, when I just used the class on every page seperately, it worked.

Index.php
<?php $page = 'one'; include('includes/header.php'); ?>
$page = 'one'. "==" is a comparison operator http://www.php.net/manual/en/language.operators.comparison.php
Header.php
<li><a href="#"<?php if($page == 'one'){ echo ' class="active"';}?>>LINK</a></li>
I think is more clean like that

just move the echo to correct place(s), because your syntax is not correct
Like this;
<li>
<a
<?php
echo( $page == 'one' ? "class='active'" : "");
?>
href="contact.php">Contact</a>
/
</li>
And also you should correct this in include.php
$page = 'one';
with single "=" sign

<?php
$page = 'one';
include('includes/header.php');
?>
$page =1; not $page == 1; because it is assignment of a variable.
Rest of the code is fine.

Related

Active current page using php not working

I have this code in header.php:
<?php
$page = "";
?>
<ul class="nav navbar-nav navbar-right nav-pos">
<li <?php if ($page == "home") { ?> class="active"<?php } ?>>Home</li>
<li <?php if($page == 'about') { ?> class="active"<?php } ?>>About us</li>
<li <?php if($page == 'contact'){ ?> class="active"<?php } ?>>Contact us</li>
<li class="hidden-xs"><i class="fa fa-search fa-rotate-90"></i></li>
</ul>
and on every page i put this code(and the name change depends on current page):
<?php include 'header.php';
$page = 'about' ?>
and in css i made this:
.active{color:red }
But it is not working... any idea why?
Thank you for time, I really appreciate it
The problem is that in your header.php you explicitly set $page = "" - which means none of the checks for $page == 'about'(etc) are passing.
Although you do set $page = 'about' in your main script, this is after the code in header.php has run, and its html output produced.
The solution is simple:
1) remove the $page = "" from header.php
2) set $page = "about" (or equivalent) before the include
You can do by using jQuery just adding this script in the footer section.
$(document).ready(function() {
// get current URL path and assign 'active' class
var pathname = window.location.pathname;
$('.nav > li > a[href="'+pathname+'"]').parent().addClass('active');
});
In your case, this will work.
$(function(){
var current = location.pathname;
$('.nav li a').each(function(){
var $this = $(this);
// if the current path is like this link, make it active
if($this.attr('href').indexOf(current) !== -1){
$this.addClass('active');
}
})
});
first you should remove $page = ""; from header
<?php
$page = "";//remove this line in header file
?>
<ul class="nav navbar-nav navbar-right nav-pos">
Second in every page swap these two lines
<?php
$page = 'about';//swap these lines
include 'header.php' ;
?>
some details: when header load then $page is empty and your conditions false in li tag. so, before header load you should assign value to $page
the best way: you should used $_SERVER['REQUEST_URI'] instead of hard coded $page. Example:
$url= explode('/',$_SERVER['REQUEST_URI']);
$page = end($request_array);//this show your_page.php
$page=preg_replace('"\.php$"', '', $page )//this will remove .php extension

Change HTML on includes on each page

I have my website, for example with this layout:
<?php include 'header.php';?>
<!-- CONTENT -->
<?php include 'footer.php';?>
Now, on each page, for example: home, contact, about us, etc, I have to add a class active to the menu. But since I have just one file with the header, how can I change some HTML in each single page?
Thanks, and sorry if this was asked before, I can't find this anywhere, and I don't know how to search for it.
Say, you 3 pages.
Get them in an array in header.php.
$pages = array();
$pages['home'] = 'Home';
$pages['about-us'] = 'About Us';
$pages['contact'] = 'Contact Us';
Show menu like and get current page parameter:
If the parameter matches your page, show active class.
<ul>
<?php
$current = isset($_GET['page']) ? $_GET['page'] : 'home';
if (! empty($pages)) {
foreach ($pages as $url => $title) {
$active = ($current == $page) ? 'active' : '';
// Observe, the class 'active', main logic is here.
?>
<li><?php echo $title;?></li>
<?php
}
}
?>
</ul>

How can I make a change in the header while I'm using the PHP include?

I'm using include for the header
and it's have this
<li><span>Arrangements</span></li>
<li><span>Build-a-Bouquet</span></li>
<li><span>Special Events</span></li>
and I want only any current opened page get the class="current"
like this
<li class="current"><span>Build-a-Bouquet</span></li>
You can do it like this:
<?php
$menus = array();
$menus['Arrangements'] = 'arrangements.php';
$menus['Build-a-Bouquet'] = 'bouquet.php';
$menus['Special Events'] = 'events.php';
?>
<ul>
<?php
$currnet_page = $_SERVER['PHP_SELF'];
if (! empty($menus)) {
foreach ($menus as $menu => $menu_link) {
$isCurrent = ($currnet_page == $menu_link) ? 'current' : '';
?>
<li class="<?php echo $isCurrent;?>"><span><?php echo $menu;?</span></li>
<?php
}
}
?>
</ul>
Try something like this:
$active = "bouquet";
include("navigation.php");
Navigation:
<li<?php if( isset($active) && $active == "arrangements") echo " class='active'";?>><span>Arrangements</span></li>
... And so on.
This is just a quick example, $_SERVER['PHP_SELF'] can do the job.
<li><span>Arrangements</span></li>
<?php
if($_SERVER['PHP_SELF'] == '/bouquet.php'){
echo '<li class="current"><span>Build-a-Bouquet</span></li>';
}else{
echo '<li><span>Build-a-Bouquet</span></li>';
}
?>
<li><span>Special Events</span></li>
There are a few ways you can do it. Choose the one you like:
1)
Set $current_ var you want and use:
<li><a<? echo isset($current_arrangements) ? 'class="current"' : ''; ?>href="arrangements.php"><span>Arrangements</span></a></li>
<li><a<? echo isset($current_bouquet) ? 'class="current"' : ''; ?>href="bouquet.php"><span>Build-a-Bouquet</span></a></li>
<li><a<? echo isset($current_special_events) ? 'class="current"' : ''; ?>href="events.php"><span>Special Events</span></a></li>
2) same as above but use one variable and check for its value (exactly the way Niet the Dark Absol wrote)
3) wrap it to some function
<?
// Echo Class If Current
function ecic($page) {
echo eregi($page, $_SERVER['PHP_SELF']) ? 'class="current"' : '';
}
?>
<li><a <? ecic('arrangements'); ?>href="arrangements.php"><span>Arrangements</span></a></li>
<li><a <? ecic('bouquet'); ?>href="bouquet.php"><span>Build-a-Bouquet</span></a></li>
<li><a <? ecic('events'); ?>href="events.php"><span>Special Events</span></a></li>
BTW: In most common PHP configurations you can use just <? instead of <?php.

Add the page name to the body tag

I have the following PHP script that will add a class of .Active to the current open page - this bit works but I am also trying to also add the page name to the body tag as an ID "#", but it does not seem to be working how I do it. Can anyone please advice me?
<!--add class .active to current page-->
<?php
$directoryURL = $_SERVER['REQUEST_URI'];
$path = parse_url($directoryURL, PHP_URL_PATH);
$components = explode('/', $path);
$currentPage = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($components));
if ($currentPage == "") {
$currentPage = "index";
}
function href($url) {
global $currentPage;
$path = explode('/', $url);
$page = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($path));
echo 'href="' . $url . '" ';
if ($page == $currentPage) {
echo 'class="active"';
}
}
?>
Here is the menu:
<li><a <?php href('index.php'); ?>>Home</a></li>
<li><a <?php href('about.php'); ?>>About</a></li>
<li><a <?php href('treatments.php'); ?>>Treatments</a></li>
And the HTML code:
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
It is very simple add that variable $page in echo
echo "class='active' id='$page'";
Basically, if you don't want to modify the template, you can't.
The solution would be to do this in your template, but it must be obvious to you:
<body <?php echo $id; ?>>
What seems less obvious to you, is just that you can't do this without touching the template.
Also, you should avoid global variables.

How do I write a basic if statement to include code on the index page?

I'm so used to writing for Wordpress that I don't know how to do a simple PHP logic statement by itself!
For a very basic project, I'm trying to include an unordered list on the index.php page. Here's an example of code that I am trying to use:
<?php
$index = $_SERVER['REQUEST_URI'];
if ($index == "/") {
?><ul id="links">
<li>Facebook</li>
<li>Example</li>
<li>Test</li>
<li>Page</li>
</ul>
<?php } ?>
I know this needs to work for index pages ending with the actual URI of index.php as well as a blank suffix without index.php. How can I write this code to work correctly?
You need to use basename() function to get actual page name like this:
<?php
$page = basename($_SERVER['REQUEST_URI']);
if ($page == 'index.php' || $page == "/" || $page == '') {
?><ul id="links">
<li>Facebook</li>
<li>Example</li>
<li>Test</li>
<li>Page</li>
</ul>
<?php } ?>
It does work however if you use SCRIPT_NAME with the slashes following.
<?php
$page = basename($_SERVER['SCRIPT_NAME']);
if ($page == 'index.php' || $page == "/" || $page == '')
{ ?>
Hi This should show up.
<?php }?>

Categories