My navigation is called on all page with code <?php include 'navigation.php'; ?>. Navigation I want to use on all pages:
<nav class="white-color">
<!-- nav code -->
</nav>
But I need to change class="white-color" on specific pages to be class="black-color". Is there a option to add simple PHP code to define:
<?php
if($page == "index") {
echo '
<nav class="first-class">
</nav>
';
} else if($page == "register") {
echo '
<nav class="second-class">
</nav>
';
} else {
/* DEFAULT CLASS FOR PAGES*/
echo '
<nav class="third-class">
</nav>
';
}
?>
Or maybe I can resolve this with the switch method. Any advice or example will be good.
Thanks all for helping me.
basename($_SERVER['SCRIPT_FILENAME'])
will return the file name that is currently in use. So if you are on localhost/index.php it will return index.php.
Furthermore you can make your code more effective by doing the following:
print "<nav class='";
if(basename($_SERVER['SCRIPT_FILENAME']) == "index.php"){
print "first-class";
}
else if(basename($_SERVER['SCRIPT_FILENAME']) == "register.php"){
print "second-class";
}
else{
print "third-class";
}
print "'></nav>";
If we take into account the comment from deceze it would look like this.
if(basename($_SERVER['SCRIPT_FILENAME']) == "index.php"){
$navClass = "first-class";
}
else if(basename($_SERVER['SCRIPT_FILENAME']) == "register.php"){
$navClass = "second-class";
}
else{
$navClass = "third-class";
}
print "<nav class='$navClass'></nav>";
Related
I am changing the color of a link after it is selected. The color is changing, but when the selected page is rendered it returns to the default color.
php pages look like this:
<!DOCTYPE html>
<html>
<?php
$Page = "Contact";
include 'header.php';
?>
<body>
<?php
include "facebook.php";
?>
<?php
include "headers.php";
?>
<?php
include "navBar.php";
?>
<?php
include "containers.php";
?>
<?php
include "footer.php";
?>
</body>
</html>
navigation bar where selection is made:
<div class="navBar">
<a class="aNavBar" href="index.php">Home</a> <a class="aNavBar" href="about.php">About</a> <a class="aNavBar" href="galleries.php">Gallery</a> <a class="aNavBar" href="equipment.php">Equipment</a> <a class="aNavBar" href="links.php">Links</a> <a class="aNavBar" href="contact.php">Contact</a>
</div>
<script>
$(document).ready(function()
{
$('.navBar a').click(function()
{
var href = $(this).attr('href'); //location.href;
alert(href);
$(this).addClass('selected');
});
});
</script>
I've tried putting code in the main (contact).php file, but the same thing is occurring.
You can also do this in following way
$path = $_SERVER['PHP_SELF']; // will return http://test.com/index.php for our example
$page = basename($path); // will return index.php
Then put condition
<a class=<?php ($page == index.php) ? echo "aNavBar selected" : echo "aNavBar"; ?> href="index.php">Home</a>
The links defined in html have standard colors, even when they are clicked or not. What defines if a link was clicked is the browser cache. As you are verified if it was selected, when the page loads this information will not be kept. It may be interesting to save the click of this link in the user's history, in a database, because through the browser it will not be possible.
Simply put, page state is not persisted after the page reloads or changes. So, you should not expect the selected hyperlink to remain 'selected' when the 2nd page is loaded. You can do it programatically though:
Something like this:
<div class="navBar">
<a class="aNavBar<?php if(strpos($_SERVER['PHP_SELF'], 'index.php')!==false) echo ' selected'; ?>" href="index.php">Home</a> <a class="aNavBar" href="about.php<?php if(strpos($_SERVER['PHP_SELF'], 'about.php')!==false) echo ' selected'; ?>">About</a>
The following worked:
<?
$path = $_SERVER['PHP_SELF']; // will return http://test.com/index.php for our example
$page = basename($path); // will return index.php
?>
<a class="aNavBar" <? if($page == "index.php") echo "style='color:red'";?> href="index.php">Home</a>
Thanks for your help!
Hello I'm trying to make my navigation bar active in php, so that user can know in which page they are on. So how can I add a class="active" in this code to make a active nav bar and display all the page in same index.php page.
<?php
return "<nav id='nav-reservation'>
<a href='resource-index.php?page=room' class='nav-reservation-item'>Room</a> |
<a href='resource-index.php?page=equipment' class='nav-reservation-item'>Equipment</a>
</nav>
";
You can have manual css for the same [http://jsfiddle.net/Ag47D/3/ ]
or
You can use bootstrap [ http://www.bootply.com/IsRfOyf0f9 ]
$(".nav b").on("click", function(){
$(".nav").find(".active").removeClass("active");
$(this).parent().addClass("active");
});
In your individual parameter such as page=room, page=equipment, you can do:
<nav id='nav-reservation'>
<a href='resource-index.php?page=room' class='nav-reservation-item <?php if( $_REQUEST['page'] == "room" ){ echo "active"; } ?>'>Room</a> |
<a href='resource-index.php?page=equipment' class='nav-reservation-item <?php if( $_REQUEST['page'] == "equipment" ){ echo "active"; } ?>'>Equipment</a>
</nav>
I'm creating a Server Side php Website application.
In my first page , the whole code is php , and I've set
$_SESSION["lang"] = 'lang.en.php';
$_SESSION["lang"] = 'lang.it.php';
When I change flag, I change a variable :
if (Trim($_SESSION["lang"])=='')
{
include 'lang.it.php';
}
if (Trim($_SESSION["lang"])=='lang.en.php')
{
include 'lang.en.php';
}
if (Trim($_SESSION["lang"])=='lang.it.php')
{
include 'lang.it.php';
}
I have the following problem
My second page .php is 50% html and 50% php because in html I put a simple dropdown menu.
My Question is : How can I pass to html my variables from another php page without using Jquery?
<li> <a class="active" href="home.php"><?php $home?></a> </li>
Variable $Home is stored in - lang.it.php -
How di I access to my variables?
P.s.
I don't want to pass all Menu's variable in the session because I think is crazy to do..
EDIT --
html
<head>
<link href="Css/Menu.css" rel="stylesheet" type="text/css">
<script>
</head>
<body>
<center ><div id="header"><center >
<ul>
<li><a class="active" href="home.php">Home</a></li>
<li>Area Download</li>
<li class="dropdown">
<a href="javascript:void(0)" class="dropbtn"
onclick="myFunction()">MenĂ¹</a>
<div class="dropdown-content" id="myDropdown">
VARIABLE 1 HERE
VARIABLE 2 HERE
VARIABLE 3 HERE
</div>
<li>Logout</li>
</li>
</ul>
</div>
<center ><div id="Realbody"><center >
<center ><div id="Footer"><center >
</html>
<?php
require_once("rpcl/rpcl.inc.php");
//Includes
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");
use_unit("styles.inc.php");
include 'parameters.php';
//Class definition
class PaginaDownload extends Page
{
public $Label1 = null;
public $LoginCss = null;
function PaginaDownloadShow($sender, $params)
{
if ((empty($_SESSION["name"])) || (empty($_SESSION["user"])))
{
header('Location: \index.php');
}
}
}
global $application;
global $PaginaDownload;
//Creates the form
$PaginaDownload=new PaginaDownload($application);
//Read from resource file
$PaginaDownload->loadResource(__FILE__);
//Shows the form
$PaginaDownload->show();
?>
I've to replace VARIABLE 1 , 2 , 3 with my variable on Lang.php
<li><a class="active" href="home.php">Home</a></li>
<li><a class="active" href="home.php"><?php $home?> </a></li>
That's my try :
//Includes
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");
use_unit("styles.inc.php");
use_unit("Zend/zauth.inc.php");
include '/parameters.php';
if (Trim($_SESSION["lang"])=='') { include 'lang.it.php'; }
else
include Trim($_SESSION["lang"]);
//Class definition
and here my lang file
<?php
/*
------------------
Language: English
------------------
*/
//Login Page #1/X
$title_page_Login = 'Login';
$Err_1_Login = 'Login Credentials Incorrect';
$Err_2_Login = 'Not Found User :';
$Err_3_Login = 'Database Offline';
$Err_4_Login = 'Insert User & Password';
$Err_5_Login = 'Need User';
$Err_6_Login = 'Need Password';
$req_user = 'Insert Username';
$req_pass = 'Insert Password';
$Bt_Label = 'Sign In';
//-------------------
//Home
$home = 'Home';
?>
I have a index file in my root directory.
I have a directory called pages.
Nav bar is in the includes directory.
If I want to include the navbar in index.php and also in each one of my pages.php which are located in the pages directory. What is the proper way to structure this nav bar so the php can move up and down directories by what page it is on.
each page is identified as
$page = currentPage
And here is my current navbar which is in the includes dir
<ul>
<?php if ($page == 'home') { ?><li class="active">Home</li>
<?php } else { ?><li>Home<?php } ?>
<?php if ($page == 'contact') { ?><li class="active">Contact Us</li>
<?php } else { ?><li>Contact Us<?php } ?>
<?php if ($page == 'services') { ?><li class="active">Services</li>
<?php } else { ?><li>Services<?php } ?>
<?php if ($page == 'employees') { ?><li class="active">Employees</li>
<?php } else { ?><li>Employees<?php } ?>
<?php if ($page == 'dashboard') { ?><li class="active">Dashboard</li>
<?php } else { ?><li>Dashboard<?php } ?>
</ul>
Directory looks like this.
root
root/includes
root/pages
Thanks for your help in advance. I realize I could just do some simple things to get around this but I really want to understand this for the future.
This is how the page is currently laid out:
<?php $page = 'contact'; ?>
<html>
<meta charset="utf-8">
<title>Contact Us</title>
<head>
<base href="http://www.mysite.com/Dir_IM_Working_on_root/pages/" />
<link rel="stylesheet" type="text/css" href="../css/styles.css" />
</head>
<body>
<div id="wrapper">
<div id="nav">
<?php include '../includes/nav.php'; ?>
</div><!-- CLOSING NAV DIV -->
<div id="main">
</div><!-- CLOSING MAIN DIV -->
<footer>
<?php include '../includes/footer.php'; ?>
</footer>
</div><!-- CLOSING WRAPPER DIV -->
</body>
</html>
Easiest would be to output absolute URLs in all cases. (/test.php)
If you want to leave the possibility to run your site in a subfolder, you can prepend all paths with a variable. This can be changed to the current position of your site.
Another way is to add a
<base href="http://www.example.org/subfolder/" />
to your html code and change that accordingly in combination with absolute URLs. All URLs are based on what you put into the attribute. This is a very elegant solution, but it affects a lot, which might be surprising if one is not that trained in web linkage.
I think there is no "right" way. Typo3 uses the base tag variant and it's most likely the way I would use. Lots of other software use the variable variant (or wrap the path in a function -- which is just another way of achieving the same). Just try some ways and decide for the one which seems most appealing to you I'd say.
In my experience the only thing nearly nobody uses are relative URLs.
Update
Use this html in combination with base html tag.
<ul>
<li<?php if ($page == 'home') echo ' class="active' ?>>Home</li>
<li<?php if ($page == 'contact') echo ' class="active' ?>>Contact Us</li>
<li<?php if ($page == 'services') echo ' class="active' ?>>Services</li>
<li<?php if ($page == 'employees') echo ' class="active' ?>>Employees</li>
<li<?php if ($page == 'dashboard') echo ' class="active' ?>>Dashboard</li>
</ul>
//checks that we are on current page and highlights tab as active if so
if(is_page($page_name)){
echo " <li><a href='$href' class='current_page_item'> $tabname</a></li>";
}
else {
}
if(is_single() && $singlelight=="this_one") {
echo " <li><a href='$href' class='current_page_item'> $tabname</a></li>";
}
else {
echo " <li><a href='$href' > $tabname</a></li>";
}
The code above works as I expected - highlight tabs using the WordPress function is_single and is_page. The problem is it generates 2 tabs for the active one in the menu. So my menu looks like this when Home is active.
Home Home Faq Blog Contact
Appreciate any help.
Do you need the first if statement?
What if you tried:
if(is_single() && $singlelight=="this_one" && is_page($page_name)) {
echo " <li><a href='$href' class='current_page_item'> $tabname</a></li>";
}
else {
echo " <li><a href='$href' > $tabname</a></li>";
}