I am working with two pages which have included the navigation menu. The problem I am having is to show which one is active. Right now Rapport stays active all the time, even if I am in the diagram. I noticed that $page is always returning Rapport, which I dont know why.
In the diagram (index.php) I have:
include('../rapport/navigasjon.html');
$page = 'diagram';
$_SESSION['diagram']= $page;
In the rapport (index.php) I have:
include('navigasjon.html');
$page = 'rapport';
$_SESSION['rapport']= $page;
And in navigasjon.html I have:
session_start();
$page = $_SESSION['diagram'];
$page = $_SESSION['rapport'];
AND:
<li class='pil <?php if($page=='rapport') {echo 'active';} ?>'> <a href="../rapport/index.php" class='rapport' >Rapport</a></li>
<li class='pil <?php if($page=='diagram') {echo 'active';} ?>' ><a href="../diagram/index.php" class='diagram' >Diagram</a></li>
set $page variable BEFORE including your navigation :
diagram/index.php :
$page = 'diagram';
$_SESSION['diagram']= $page;
include('../rapport/navigasjon.html');
and rapport/index.php :
$page = 'rapport';
$_SESSION['rapport']= $page;
include('navigasjon.html');
In navigasjon.html remove these lines :
$page = $_SESSION['diagram'];
$page = $_SESSION['rapport'];
If you are trying to set a class to an active menu, you can try to this using directories names.
<?php
$pathInfo = pathinfo($_SERVER['PHP_SELF']);
$page = strtolower(end(explode('/', $pathinfo['dirname'])));
?>
<li class='pil <?php if($page=='rapport') {echo 'active';} ?>'> <a href="../rapport/index.php" class='rapport' >Rapport</a></li>
<li class='pil <?php if($page=='diagram') {echo 'active';} ?>' ><a href="../diagram/index.php" class='diagram' >Diagram</a></li>
Related
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.
I am working in PHP website. I am having menu with select option on corresponding pages. Here are my code :
PHP
<?php
$path = $_SERVER['PHP_SELF'];
$page = ltrim(str_replace("/", "", dirname($path)."/".basename($path, '.php')),"/");
?>
<div id="navbar-collapse-grid" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="<?php echo SITE_PATH; ?>" <?php echo ($path == "/index.php") ? 'class="select"' : ''; ?>>Home</a></li>
<li><a href="<?php echo SITE_PATH; ?>about/overview.php" <?php echo ($path == "/about/overview.php") ? 'class="select"' : ''; ?>>About Us</a></li>
</ul>
</div>
Everything is work fine. If my url as "www.example.com/index.php", Home as SELECT. "www.example.com/about/overview.php", then About SELECT.
1) My Question is how to write code or how to check my path as "www.example.com" instead of "www.example.com/index.php". Because, all you know, i need a home page link as "www.example.com/" and not as "www.example.com/index.php"
2) How to redirect this as in .htaccess file and where to put and how to conect this .htaccess file.
How do you hide specific content if $page = name
For example:
<?php
if ($page=='special'){
echo "<div>hello</div>";
}
?>
The above example will show the div if the $page = special. How do I do the opposite of this, hide a specific div if the $page = something?
Edit:
To be more specific I would like to hide my main navigation when on the $clients page.
Do I wrap the <nav> with PHP or is it possible to hide a specific div if I give it a name, for example. <nav id="clients"> the PHP would be: if $clients then hide the id named clients.
I should also mention that the content in question has <?php echo $url; ?> and the likes contained within it.
This is the exact content I would like to hide on $clients pages.
<nav>
<ul>
<li><a <?php if ($page=="work") echo "class=\"current\"" ?> href="<?php echo $url; ?>" title="Work">Work</a></li>
<li><a <?php if ($page=="profile") echo "class=\"current\"" ?> href="<?php echo $url; ?>profile/" title="Profile">Profile</a></li>
<li><a <?php if ($page=="approach") echo "class=\"current\"" ?> href="<?php echo $url; ?>approach/" title="Approach">Approach</a></li>
<li><a <?php if ($page=="contact") echo "class=\"current\"" ?> href="<?php echo $url; ?>contact/" title="Contact">Contact</a></li>
</ul>
</nav>
hide a specific div if the $page = something?
if ($page !='special'){
echo "<div>hello</div>";
}
You just echo it if $page is different from something.
If you want, you can also echo it anyway but as 'hidden' if that's what you're trying to achieve.
if ($page =='special'){
echo "<div>hello</div>";
} else {
echo "<div style='display:hidden;'>hello</div>";
}
This way, the div will be in the DOM anyway and you can show it later on without reloading the page using JavaScript.
If you want it on every other page that is not special
<?php
if ($page!='special'){
echo "<div>hello</div>";
}
?>
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.
I have a page which get's populated dynamically from a Mysql database.
To prevent to much information been displayed at once, I have added pagination to page.
Using this code.
$currentPage = $_SERVER["PHP_SELF"];
$maxRows_atoz = 10; $page= 0;
if (isset($_GET['page'])) {$page= $_GET['page'];}
$startRow_atoz = $page* $maxRows_atoz;
mysql_select_db($database_main, $main);
$query_atoz = "SELECT * FROM main WHERE title LIKE 'A%'";
$query_limit_atoz = sprintf("%s LIMIT %d, %d", $query_atoz, $startRow_atoz, $maxRows_atoz);
$atoz = mysql_query($query_limit_atoz, $main) or die(mysql_error());
$row_atoz = mysql_fetch_assoc($atoz);
if (isset($_GET['totalRows_atoz'])) {$totalRows_atoz = $_GET['totalRows_atoz'];} else {$all_atoz = mysql_query($query_atoz); $totalRows_atoz = mysql_num_rows($all_atoz);}
$totalPages_atoz = ceil($totalRows_atoz/$maxRows_atoz)-1;
$queryString_atoz = ""; if (!empty($_SERVER['QUERY_STRING'])) {$params = explode("&", $_SERVER['QUERY_STRING']); $newParams = array(); foreach ($params as $param) {
if (stristr($param, "pageNum_atoz") == false && stristr($param, "totalRows_atoz") == false) {array_push($newParams, $param);}}
if (count($newParams) != 0) {$queryString_atoz = "&" . htmlentities(implode("&", $newParams));}}
$queryString_atoz = sprintf("&totalRows_atoz=%d%s", $totalRows_atoz, $queryString_atoz);
?>
<ul>
<li class="previous"> <?php if ($page > 0) { // Show if not first page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, max(0, $page - 1), $queryString_atoz); ?>">
<img src="/files/previous.png" /></a>
<?php } // Show if not first page ?></li>
<li class="next"> <?php if ($page < $totalPages_atoz) { // Show if not last page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, min($totalPages_atoz, $page + 1), $queryString_atoz); ?>">
<img src="/files/next.png" /></a>
<?php } // Show if not last page ?></li>
</ul>
The code works in paginaing the results, but has a limitation.
It currently only display's Next and Previous buttons.
I would like to add page numbers. This way a user can skip to 4th or 5th page in the result set rather than having to press the next button 4 or 5 times.
So, can some please tell me how I would go about doing this?
Thanks
I must say, I havent gone through the entire code of yours. You must indent your code to help others. If I get your question right, the following may help you. I haven't much taken care of finer details. This is just to give you an idea. :-)
<ul>
<li class="previous">
<?php if ($page > 0) { // Show if not first page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, max(0, $page - 1),$queryString_atoz); ?>">
<img src="/files/previous.png" /></a>
<?php } // Show if not first page ?></li>
<!--MODIFY_BEGIN-->
<li>
<?php for ($i=0;$i<$page; $i++){
if ($currentPage==$i)
echo ($currentPage+1);
else
echo "<a title='Page $i' href='?page=$i'>Page $i</a>";
} ?>
</li>
<!--MODIFY_END-->
<li class="next"> <?php if ($page < $totalPages_atoz) { // Show if not last page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, min($totalPages_atoz, $page + 1), $queryString_atoz); ?>">
<img src="/files/next.png" /></a>
<?php } // Show if not last page ?></li>
</ul>