<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'); ?>
Related
I'd like to add condition to my code so that after click on label it link me to other page. Now this code link to other article on the website. So I guess what should I add to get from my label to other page.
Thank you for answers!
My code is below:
<ul class="nav nav-tabs aboutUsTabs nav-justified" role="tablist">
<?php foreach ($this->items as $key=>$item ){
if($item['children']){ ?>
<li role="presentation" class="<?php if($item['menu']->id==$this->pageId || $item['menu']->id==$this->parentId){ echo 'active'; } ?>"><?php echo $item['menu']->title;?></li>
<?php }else{ ?>
<li role="presentation" class="<?php if($item['menu']->id==$this->pageId || $item['menu']->id==$this->parentId){ echo 'active'; } ?>"><?php echo $item['menu']->title;?></li>
<?php } ?>
<?php } ?>
</ul>
All this start/stop of PHP code hurts my eyes! Anyways, linking to other pages is done with tags. Not quite sure I understand your question, but you could (if you mean labels for form elements) do something like this;
<label for=""> Something Meaningful </label>
I'm sorry, but I took the liberty to clean up your code a little. Hopefully this will make it more readable;
<ul class="nav nav-tabs aboutUsTabs nav-justified" role="tablist">
<?php
foreach ($this->items as $key=>$item ){
$active = NULL;
if($item['menu']->id==$this->pageId || $item['menu']->id==$this->parentId){
$active = 'active';
}
$menu = NULL;
if($item['menu']->id==$this->pageId || $item['menu']->id==$this->parentId){
$menu = 'active';
}
if($item['children']){
echo'<li role="presentation" class="'.$active.'">
'.$item['menu']->title.'
</li>';
}else{
echo'<li role="presentation" class="'.$menu.'">
'.$item['menu']->title.'
</li>';
}
}
?>
</ul>
For your comment question, if you know what item needs a different value, it could be the title f.ex. then you can check against that value and change the output of your element. Maybe something like;
if($item['children']){
if($item['title'] == 'Unique Identifier for your element') {
// In here you could manipulate the output of that one item you want to exclude/change
} else {
// Your normal output
echo'<li role="presentation" class="'.$active.'">
'.$item['menu']->title.'
</li>';
}else{ ... }
And even better would be to perform the check beforehand, so maybe something like;
$link = '#about-us-page-'.$item['menu']->id;
if($item['title'] == "That identifier") {
$link = 'somethingElse';
}
and then change the value of your href tag to;
'.$item['menu']->title.'
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.
I'm using PHP, MySQL, HTML and CSS. Depending upon value in my PHP variable I want to apply a CSS class to an element. How should I do this? My code snippet of PHP and HTMl( element) is as follows:
<?php
$e=$_POST['users']; //$e should have either employee_id or a string "All"
?>
<li><p align="center">Salary Report(Individual)</p></li>
<li><p align="center">Salary Report(Combined)</p></li>
Now the scenario is if $e=="All" then I have to apply a CSS class "class="active" to following
<li><p align="center">Salary Report(Combined)</p></li>
and if not then aapply the same CSS class to the other
<li><p align="center">Salary Report(Individual)</p></li>
My issue is how should I use a condition depending upon PHP variable and apply a specific class to an HTML element? Can anyone help me in this regard? Thanks in advance.
Try with the ternary operator like
<li class="<?php echo $e == "All" ? 'active' : ''; ?>><p align="center">Salary Report(Combined)</p></li>
You can do it by following code.
<?php
if($e=="All")
{
?>
<li class="active"><p align="center">Salary Report(Combined)</p></li>
<?php
}else{
?>
<li class="active"><p align="center">Salary Report(Individual)</p></li>
<?php
}
?>
You could do it like this:
<li class="<?php echo $e == "all" ? 'active' : ''; ?>><p align="center">Salary Report(Combined)</p></li>
I don't understand why nobody says about second <li>. Just flip the statements to make another <li> "active"
<?php
$e=$_POST['users'];
?>
<li<?=($e=='All'?'':' class="active"')?>><p align="center">Salary Report(Individual)</p></li>
<li<?=($e=='All'?' class="active"':'')?>><p align="center">Salary Report(Combined)</p></li>
<li class="<?php echo $e == "All" ? 'active' : ''; ?>><p align="center">Salary Report(Combined)</p></li>
You can also approach this way.. This is for better understanding of what is doing out there.
<?php
$data = ($e == "All") ? 'active' : '';
?>
<li class="<?php echo $data; ?>"><p align="center">Salary Report(Combined)</p></li>
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':''?>">
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.