Using a querystring to highlight a menu item - php

So I'm trying to use a query string to highlight a 'current' menu item.
Say the url is www.....something.php?tag=Music
And I'm looping through this code to check the $tag against a record in the database:
<li class="<?php if(isset($_GET['tag']) && $_GET['tag'] == $record->name);
{ echo 'current'; }?>">
<a href="?tag=<?php echo $record->name; ?>">
<?php echo $record->name; ?></a></li>
Why doe's it always come out 'true' and echo 'current'.
The html it outputs is this:
<li class="current">
<a href="?tag=Music">Music</a>
</li>
<li class="current">
<a href="?tag=Film">Film</a>
</li>
<li class="current">
<a href="?tag=biscuits">biscuits</a>
</li>
Surely it should only be 'true' for 'Music'?

You have a semi-colon after your if statement. Remove that and it should work:
<li class="<?php if(isset($_GET['tag']) && $_GET['tag'] == $record->name)
For shorter code, and if you have short tags enabled, try:
<li class="<?=isset($_GET['tag'])&&$_GET['tag']==$record->name?'current':''?>">

Related

<a href> get value in php

The current page someone is in on my website is highlighted on the navigation menu (the class "active"). I've done this through php, which compares the current page to a string. I'd like to simplify it even further because this string is actually the href value of the menu item.
Is there a way to get the href value of this item via php? That way I could just use that variable instead.
My php code:
<?php
function curPageName()
{
return substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"], "/") + 1);
}
function activeMenuItem($href)
{
return (curPageName() == $href) ? "class=\"active\"" : "";
}
?>
and the html in the body:
<ul id="navilist">
<li><a href="index.php" <?php echo activeMenuItem('index.php'); ?>>Home</a></li>
<li><a href="resume.php" <?php echo activeMenuItem('resume.php'); ?>>Resume</a></li>
<li><a href="projects.php" <?php echo activeMenuItem('projects.php'); ?>>Projects</a></li>
<li><a href="contact.php" <?php echo activeMenuItem('contact.php'); ?>>Contact</a></li>
</ul>
Build your menu in a loop:
$menu_items = array(
'Home' => 'index.php',
'Resume' => 'resume.php'
);
<ul id="navilist">
<?php foreach ( $menu_items as $title => $href ): ?>
<li><a href="<?php echo $href; ?>" <?php echo activeMenuItem($href); ?>>
<?php echo $title; ?>
</a></li>
<?php endforeach; ?>
</ul>
You Can't get the href value using php code.
in my knowledge - one way to get the href value
You should declare the page name with one one variable like
$home="home.php";
$resume="resume.php";
.
.
.
<ul id="navilist">
<li><a href="<?php echo $home;?>" <?php echo activeMenuItem($home); ?>>Home</a></li>
<li><a href="<?php echo $resume;?>" <?php echo activeMenuItem($resume); ?>>Resume</a></li>
.
.
.
</ul>
I am not sure. Please check it.

PHP Dynamic menu highlight from database

I've been making a website for a client that is based off of 1 page and the links are to different categories or articles. I'm showing the content depending on the URL parameter like this,
<a href="index.php?cat_id=<?php echo $category['cat_id']" >
usually to do a navigation highlight depending on the page I would do something like this,
PHP
<ul>
<li <?php if($pagename == "index.php"){ echo 'class="selected"'; } ?>>
Home
</li>
<li <?php if($pagename == "about.php"){ echo 'class="selected"'; } ?>>
About
</li>
<li <?php if($pagename == "services.php"){ echo 'class="selected"'; } ?>>
Services
</li>
<ul>
but my URLS are dynamic from the database like so,
PHP
<ul>
<li>Home</li>
<?php while ($category = $statement->fetch()) { ?>
<li><?php echo $category['cat_name']; ?></li>
<?php
}
?>
</ul>
So I was wondering how can I add the class selected to the links cat_id when the URL contains that parameters id? Thanks in advance for any help!
If the category IDs do not change over time:
if($_GET['category_id'] == "1" ){ echo 'class="selected"'; }
If the categorie IDs are subject to change over time, you will need to perform a pre-query to get a map of category IDs to pages. Then you can do something like:
if($_GET['category_id'] == $categories['home_page'] ){ echo 'class="selected"'; }
<?php if($category['cat_id']==$_GET['cat_id']){ echo 'class="selected"';}?>

Simple php quote syntax error on echo usage inside echo

I have no problem with creating php submenu class selected but when i try to create this submenu inside submenu: I can't get it work.
Problem: Defining echo inside echo returning syntax error because of quotes.
<ul class="sub_nav">
<li <?php if ($page=='kurumsal-hakkimizda') {echo "class='selected'";} ?>>
Hakkımızda
</li>
<li <?php if ($page=='kurumsal-ik') {echo "class='selected'";} ?>>
İnsan Kaynakları
<?php
if ($page=='kurumsal-ik')
{ echo '
<ul id="sub_sub_nav">
<!-- !! PROBLEM STARTS HERE !! -->
<li class="'if($page=='kurumsal-ik'){echo 'selected'}'">
<!-- !! CANT USE ECHO INSIDE ECHO BEACUSE OF QUOTES !! -->
İnsan Kaynakları Politikamız
</li>
<li class="'if($page=='kurumsal-hedef'){echo 'selected'}'">
Kurumsal Hedef
</li>
</ul>
';}
?>
</li>
<li <?php if ($page=='kurumsal-haberler') {echo "class='selected'";} ?>>
Kurumsal Haberler
</li>
</ul>
Its already inside echo right? So do this:
<li class="', ($page=='kurumsal-ik') ? 'selected' : '', '">
It works for sure. It is called ternary operator! :)
Note: Dot concatenation operator cannot be used here, because, the ternary operator acts as a function returning a value. Only comma , can be used.
You could just make the variable and echo it out at the very end:
<?php
if ($page=='kurumsal-ik')
{
$myVar= '
<ul id="sub_sub_nav">
<li class="';
if($page=='kurumsal-ik')
{
$myVar .= 'selected';
}
$myVar.='">
İnsan Kaynakları Politikamız
</li>
<li class="';
if($page=='kurumsal-hedef')
{
$myVar.= 'selected';
}
$myVar.='">
Kurumsal Hedef
</li>
</ul>
';
echo $myVar;
}
?>
As Praveen has said, it's already within an echo so there is no need to use another.
I believe the syntax for a ternary operator within an echo is:
<?php echo '<li class="' . ( $page == 'page_name' ? 'selected' : '' ) . '">Link</li>'; ?>
Hope that helps.
One the useful features of PHP is that it directly outputs everything that is outside the <?php and ?> tags, so you don't need long echo statements.
This is particularly practical when used in combination with the alternative syntax for control structures
Your code would be much more readable like this:
<?php if($page=='kurumsal-ik'): ?>
<ul id="sub_sub_nav">
<li class="<?php if($page=='kurumsal-ik') echo 'selected';?>">
İnsan Kaynakları Politikamız
</li>
<li class="<?php if($page=='kurumsal-hedef') echo 'selected';?>">
Kurumsal Hedef
</li>
</ul>
<?php endif; ?>
P. S.
You can shorten your code by using the shorthand <?=, which means <?php echo.

PHP set active link on page using includes

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.

what's wrong with my php code?

<li><a class="<?php if($_GET['q']=='addons.html');echo 'current';?>" href="addons.html">test</a></li>
<li><a class="<?php if($_GET['q']=='technicalsupport.html');echo 'current';?>" href="technicalsupport.html">example</a></li>
<li><a class="<?php if($_GET['q']=='center.html');echo 'current';?>" href="center.html">center</a></li>
<li><a class="<?php if($_GET['q']=='about.html');echo 'current';?>" href="about.html">about</a></li>
why those are all add the "current" the the current page? i want to add the class="current" only to the current page
Remove the ; after the closing ) of your if statement. i.e.
<?php if($_GET['q']=='about.html');echo 'current';?>
…becomes:
<?php if($_GET['q']=='about.html') echo 'current';?>
I agree with bradley.ayers, however I would also enclose the entire class tag within the If condition, like so.
<a <?php if($_GET['q']=='addons.html') echo 'class="current" ';?>href="addons.html">
This way all anchors whereby the condition is not true don't end up containing an empty class="" tag
First, check if $_GET['q'] exists, so you won't get an "undefined index" notice.
<li>
<a href="addons.html" <?php echo (isset($_GET['q']) && $_GET['q'] == 'addons.html' ? 'class="current"' : ''); ?>>test</a>
</li>
I would do it a bit cleaner like so:
<?php echo ($_GET['q']=='about.html') ? 'current': ''; ?>
or even optimize it into a function:
<?php
function set_current($page){
return ($_GET['q'] == $page) ? 'current': '';
}
?>
and your lines then becoming cleaner:
<?php echo set_current('about.html'); ?>

Categories