My website has a simple navigation using a styled <ul> list, with the current page's link highlighted. Currently, I do this by giving the <a> object a CSS class like this:
<ul class="bd-nav">
<li>Home</li>
<li>Contact</li>
</ul>
with the corresponding CSS:
.bd-nav-active {
background-color: #563a64;
}
This works perfectly. However, I would like to build the website with PHP and have a seperate file for the header/navigation and then just <?php include ?> that file on every other page.
Is there a way to dynamically set the class of the navigation links, depending on which page you're on? What would be the best approach here?
Solved after some fiddling! I simply put the class attribute into a variable like this: (make sure to escape the quotation marks!)
<?php
$nav_active = "class=\"bd-nav-active\""
?>
Then used that variable in my navigation like this:
<ul class="bd-nav">
<li><a href="index.html" <?php if ($pid == 1) echo $nav_active; ?>>Home</a></li>
<li><a href="contact.html" <?php if ($pid == 2) echo $nav_active; ?>>Contact</a></li>
</ul>
And then on the respective pages, I simply set the $pid variable:
<?php $pid = 1; ?>
Works perfectly! Thanks for the helpful answers!
#Arrabidas92 had a nice way to automatically do this by getting the page URL, but I think I'll be doing it like this to have better control over how the navigation looks.
The menu needs to be dynamically generated and each page needs to have a unique id or something at the top of the page.
When you dynamically generate the menu, insert and if clause that will echo the active class if generated menu item is the same with current page.
I have not coded in php for some time now but i use to do something like this, in each page give a unique file name at the top. For example in the home page:
<?php
$file_name = "index.php";
?>
and then in the included file apply a logic like this:
<?php
if($file_name == "index.php"){
//Your navigation for the home page
}
else if($file_name == "about.php"){
//Your navigation for the about page
}
?>
Alright so I will give you some tips. First, to know on wihch page of your website you are you can use in PHP a superglobal called $SERVER with this attribute : $_SERVER['REQUEST_URI'].
By calling this, PHP is going to return the parts of the url after your domain.com. For example, if the current url was domain.com/home, echo will return only '/home'.
Then, you can place your logic about highlighting your links :
if($_SERVER['REQUEST_URI' == '/home') {
//Add active class to the link HOME
}...
You can try this one, but this is a Jquery, just put the CDN in your file.
$(document).ready(function(){
if(window.location.href === "index.php") {
$(".bd-nav").addClass("active-bgRed");
}
else if(window.location.href === "about.php") {
$(".bd-nav").addClass("active-bgBlue");
}
else if(window.location.href === "contact.php") {
$(".bd-nav").addClass("active-bgGreen");
}
});
// CSS
.active-bgRed{
background-color: red;
}
.active-bgBlue{
background-color: blue;
}
.active-bgGreen{
background-color: green;
}
// JQuery CDN
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
Related
I have a page with a menu on for logged in users
i am including this page on all the other pages in my site but i don't want users that are NOT logged in to be able to click the links
How can I disable all the links on that page if a PHP variable = 'no'
i know i can use
if($php_var == 'no') {
//do something here
}
but I'm not sure how to disable the links?
Is there any way using CSS or Javascript to disable links?
try this
if($php_var == "no")
{
echo 'Your Text For Link';
}
else
{
echo 'Your Text For Link';
}
user javascript:void(0); for no redirection. this will maintain your css for link like others but when you click it won't redirect.
If i understood everything correctly, the answer is quite simple. Why dont you just replace the links with plain strings?
if($php_var === "no") {
echo "This is the text of your link.";
} else
{
echo "This is the text of your link.";
}
But as already mentioned, completely hiding the links is better, as usual users gets confused by such things.
this will remove all href from a tags. If php var is no. Put this code after all a tags else won't work
<?php
if($php_var === "no"){
echo '<script>var x=document.getElementsByTagName("a");for (i=0;i<x.length;i++){x[i].removeAttribute("href");}</script>';
}
?>
You would need to do the processing pre-output, PHP will not dynamically disable the href of an already created DOM element.
If you are producing the output of the links via PHP, you could do something like:
echo 'Link';
Otherwise, you could create an AJAX call to the PHP script, and if it returns 'no', iterate through your pages links and disable the links via JavaScript.
<a href='<?php echo ($php_var == "no") ? "javascript:void(0)" : "link.php" ?>'>
Hello user
</a>
you could do this:
// define if you want to make links work
$linking = true;
Then your link:
<a <?php if($linking == true) { ?> href="..." <?php } ?>>Link</a>
If links are not shown, I'd also add some CSS:
.link_that_is_no_link {
text-decoration: none;
cursor: default
}
How do you check if the user is logged in or not? Do you use sessions? The same way you check for the user if he is logged in you can decide to show items or not.
You can do both:
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
echo 'LINK';
}
that will keep showing the link but will lead nowhere if the user is not logged in.
Or you can do :
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
//do nothing here or put a link to the login page
}
that will show the link only if you are logged in.
I prefer the second option since I think that no users will like to see a link without being able to open it.
Note that code in this answer is just a guess of your real code
You can use PHP if-else condition and write HTML like this:
<a href="" onclick="return false;">
I'm trying to build a personal website, (fairly new to HTML and PHP) and am having trouble building a dynamic menu bar. By dynamic, I mean that I want the page that the use is on be highlighted.
Right now I have a horizontal menu on my page (Home, Contact, etc...), and have the CSS set up so that when one of the links has the attribute class="active", then the item remains highlighted (so the user knows which page he or she is on).
For example, in a static HTML page I would copy and paste the following code to each other static page and just change where the class="active" attribute is to align with the proper page:
Home
Page Two
Contact
I obviously want to use PHP to be able to minimize the amount of duplicated code I have scattered around.
So far, I have followed the first answer on this page It has worked great. I am able to click on a menu item and the content comes up in the body of the page. However:
I can't get it to dynamically highlight the menu since the menu options (the <a href /> lines) are not being dynamically created through PHP.
When I go to the page directly through index.php, I get an error:
Notice: Undefined index: 'page' in C:\xampp\htdocs\index.php on line 43
Obviously, when I go the page directly, the ?page=home variable is not set in the line:
Home
So, the 'page' variable in GET has not been set. I've gotten around that with an if statement that checks if it is not set and sends the home page html. However, I don't think this is the best way to do it, and when I try to tackle part b), I'm thinking I need to change this completely. My entire PHP script is like this:
<?php
$current_page = 'home';
$pages = array('home', 'pagetwo', 'contact');
function setActiveHeader() {
global $current_page;
global $pages;
$arr_length = count($pages);
for($x=0;$x<$arr_length;$x++) {
if($pages[$x] == $current_page) {
echo "$pages[$x]";
} else {
echo "$pages[$x]";
}
}
}
function putPage($page) {
// put a list of allowed pages here
global $pages;
$page = trim($page);
$page = (in_array($page, $pages)) ? $page : 'home';
// set current page global variable
$GLOBALS['current_page'] = $page;
// output contents of page selected
echo #file_get_contents('.\html\\' . $page . '.html');
}
?>
I just try to call setActiveHeader() from the HTML, but that doesn't work right. The menu is output correctly, but the the correct item doesn't get highlighted, it just stays stuck on the home option highlighted. Not sure why it is not being updated. Is there a better way to go about doing this?
Your code only goes up to 37 lines, and we can't much without the line that the error is referencing, but I'll try my best.
Basically, what Undefined Index means, is that you're trying to access an element in an array that isn't set. I'm guessing that you're trying to access $pages['page'].
There are two ways to fix this. Add page to the $pages array on the fourth line:
pages = array('home', 'pagetwo', 'contact', 'page');
Or wrap the 43rd line with an if statement:
<?php
$pages = array('home', 'about');
if (isset($pages['page'])) {
echo $pages['page'];
}
?>
However, a far easier method would be to use CSS:
home.php
<html>
<head>
<title>Foo</title>
</head>
<body id="home">
<ul class="menu">
<li id="link-home"><a>Home</a></li>
</ul>
</body>
</html>
style.css
body#home {
// active link styling here
}
So I have this code that detects what page the user is on and then spits out a class of "active" if necessary.
<li <?php if (stripos($_SERVER['REQUEST_URI'],'index.php') {echo 'class="active"';} ?>>
So just to clarify, this code checks if the url has index.php in it, and if it does, it spits out the "active" class. What I need to do and don't know how to is add multiple instances to this code. So instead of just detecting index.php it needs to be able to detect other pages like about.php for example.
Sorry if this comes a very simple question to most of you but I am new to PHP.
Split your code from the layout.
Possible solution:
<?php
$active_flags = array('index.php','about.php','test.php');
$active = '';
foreach($active_flags as $item) {
if(stripos($_SERVER['REQUEST_URI'],$item)!==false) {
$active='active';
break;
}
}
?>
<li class="<?php echo $active?>">Your list Item</li>
As you are listing manually you just enter this one it really help you
<li
if(strpos($_SERVER['REQUEST_URI'],'index.php'))
{
echo 'class="active"';
}
else
{
echo 'class="inactive"';
}
</li>
I have a situation here. In my project the menu section contains some anchor tags, it will work perfectly when we in index page, but moving to other pages i want to give the real links there, so my question is how to check which page is viewing or how to check the the site viewer is not in index page
<li>Home</li>
<li>About Us</li>
I want to change href conditionally, for example when I'm in index the above href attribute is OK, and when I'm in another page, for example register, then the href attribute change to index.php/site/index#home
Thanks in advance
UPDATE :
thank you uttara,I found a solution with the help of her
<?php
$url = $_SERVER["REQUEST_URI"];
$page = pathinfo($url);
$filename = $page['filename'];
$href = ($filename=='root_directory' || $filename=='index' || $filename=='site')?'':Yii::app()->request->baseUrl;
?>
Home
You can use the CMenu widget, and this takes care of the highlights, appearance, etc. Plus you can use custom CSS afterwards.
<?php
$this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=> 'YOUR_URL#home'),
array('label'=>'About Us', 'url'=>'YOUR_URL#about_us)'
)
);
?>
$url = $_SERVER["REQUEST_URI"];
$page = pathinfo($url);
$filename = $page['filename'];
$filename will give you the name of current page being viewed
and you can check for
if($filename != 'index')
{
echo '<li>Home</li>
<li>About Us</li>';
}
else
{
echo '<li>Home</li>
<li>About Us</li>';
}
remember $filename gives you just the filename without extension
first give the id to anchor tag and then put condition like below
<?php
// the php code
$flag=strpos('index.php',$_SERVER['PHP_SELF'])
{
?>
<script>
$(document).ready(function(){
$("#idofanchortg").attr('href','hrefyou want to add');
});
</script>
<?php } ?>
you can use CMenu widget provided by Yii
http://www.yiiframework.com/wiki/211/creating-a-css-driven-drop-down-menu-using-cmenu/
First of all I prefer to leave responsibility in just one place.
<li>Home</li>
<li>About Us</li>
I prefer this way 'couse code is more clean. Well. Now we know that all our controllers extends Controller class (/protected/components/Controller.php). In this place we can add
public function getHomeHrefLink() {
// when I'm in index page the href attribute is #home
// when I'm in register page the href attribute is index.php/site/index#home
}
So come on: We can you $this->action (for controller name) and $this->action->id (for action name) to understand where we are:
public function getHomeHrefLink() {
return $this->createUrl('index/site', array());
// when I'm in register page the href attribute is index.php/site/index#home
return '#home;
}
I am newbi to wordpress. I have created a website in wordpress which has totally 6 pages.
here 5 pages use the same header, but only one page has different header. So i how can how to create new header.
header.php(is used by all 5 pages): In this case is used "get_header();" in page.php to call the header.
home.php(to be used by home page). here is ther any possible way to access these new header
Please check the link
if link is www.test.com/home.php
then use
<?php if($_SERVER['REQUEST_URI'] == 'home.php') { ?>
<h1>Header for the Homepage</h1>
<?php } else { ?>
<h1>Header for other pages</h1>
<?php } ?>
There's different ways to do this. The easiest would be to use is_home() in header.php to check if the currently loaded page is the homepage. You could change the header.php to something like:
<?php if(is_home()) { ?>
<h1>Header for the Homepage</h1>
<?php } else { ?>
<h1>Header for other pages</h1>
<?php } ?>