Possible to include several pages in the == page_name check in PHP? - php

I'm writing a simple menu in PHP.
Its used to set class="active" in CSS.
However, I have a dropdown menu, consisting of 2 menu-items.
I would like this dropdown menu to get activated as long as pagename is either of the 2 menu items.
As you see I have found a solution using 2 separate PHP functions. However I would like to know how similar would work as just 1 PHP function :)
<li class=" <?php echo ($page_name=='personas.php')?'active':'';?> <?php echo ($page_name=='scenarier.php')?'active':'';?> dropdown">`

You could use in_array() (http://us1.php.net/in_array)
<li class="<?php echo in_array($page_name, array('personas.php', 'scenarier.php')) ?'active':'';?> dropdown">

What you need is the OR operator.
if($page_name == 'personas.php' || $page_name == 'scenarier.php'){
echo 'active';
}

Related

Highlight a list element when I'm browsing the very page

I saw a few tutorial about this problem but none of them satisfied me. I want to highlight the single element of my list which matches the page that I'm browsing. I created the code with php, is a wordpress based website, and the code actually works because when I echo the uri which I'm on it will display the right uri, but the if statement I created to add a class when I'm on the website won't output anything.. and I don't understand why.. anyway.. here's the code:
header.php
<ul class="nav nav-pills sliding" id="Jcollapse">
<li class="<?php if ($current == "/why-chickapea/"){ echo "current";}?>"><span>Why Chickapea?</span></li>
<li class="<?php if ($current == "/our-pasta/"){ echo "current";}?>"><span>Our story</span></li>
<li class="<?php if ($current == "/shop-chickapea/"){ echo "current";}?>"><span>Shop</span></li>
<li class="<?php if ($current == "/recipes/"){ echo "current";}?>"><span>Recipes</span></li>
<li class="<?php if ($current == "/blog/"){ echo "current";}?>"><span>Blog</span></li>
</ul>
In each page I added a php snippet:
<?php $current = $_SERVER["REQUEST_URI"]; ?>
If I echo the var $current I will obtain the right url in this format: /pagename/
At the end I style the class current with a yellow color
.current {
color:yellow;
}
.current a {
color:yellow;
}
Does anyone know where my mistake is?
this is the page website: choosechickapea.com
As you can see the class that my code will generate is empty, but if I echo each value the uri I will obtain is the right one
The simplest explanation would be, that you print the header before $current is set.
The second simplest explanation is different scopes, meaning either you set $current in a non-global scope or you read it in a non-global scope, and those two (whatever they are) are different. Since someone said wordpress, I guess there is some encapsulation into functions (thus changing the scope). Using the global keyword may be a solution, but a dirty one. But since you're already avoiding wordpress functions ...
The actual code is:
Before declaring in the header the if statement, SET the value of the variable. If you'll declare in the body, even before loading the header with a, for example require once or in wordpress:
<?php get_header(); ?>
It won't work, the variable has to be set in the header like this:
<?php $current = $_SERVER["REQUEST_URI"]; ?>
<header class="navbar-fixed-top">
<ul class="nav nav-pills sliding">
<li class="<?php if ($current == "/your-url/"){ echo "current";}?>"><span>your url</span></li>
<li class="<?php if ($current == "/other-url/"){ echo "current";}?>"><span>/other url/</span></li>
</ul>
</header>

See if one item in array is true

I'm trying to put a menu together where it will sense what page is loaded. This site is not done in a CMS and is straight PHP/HTML code.
I currently have the navigation working for the primary links. There is a dropdown where I am having problems. I need to be able to see if the parent or any dropdown children are active. If one of the children are active. In the example below this is "FAQ" and the children are "FAQ1," "FAQ2," and "FAQ3."
For this example I'm using a CSS state called "active."
<style>
a{color:red;}
.active{color:blue;}
</style>
Here is the script used for the menu. The links for Home, Products, and Contact are working as expected.
<ul>
<li><a href="index.php" id="homenav" <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="active"';?>>Home</a></li>
<li><a href="products.php" id ="prodnav" <?php if (strpos($_SERVER['PHP_SELF'], 'products.php')) echo 'class="active"';?>>Products</a></li>
<li><a href="faq.php" id ="faqnav" <?php if (strpos($_SERVER['PHP_SELF'], 'faq.php, faq1.php, faq2.php, faq3.php')) echo 'class="active"';?>>FAQ</a>
<ul>
<li>FAQ1</li>
<li>FAQ2</li>
<li>FAQ3</li>
</ul>
</li>
<li><a href="contact.php" id ="connav" <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="active"';?>>contact us</a></li>
</ul>
Can I please get some help on how I should be writing this line to let it work the way the others are?
<li>
<a href="faq.php" id ="faqnav"
<?php
if (strpos($_SERVER['PHP_SELF'], 'faq.php, faq1.php, faq2.php, faq3.php'))
echo 'class="active"';
?>
>FAQ</a>
</li>
strpos() only accepts one string per parameter, you can not give it a list.
Try this:
<?php if (in_array(basename($_SERVER['PHP_SELF']), array('faq.php', 'faq1.php', 'faq2.php', 'faq3.php'))) echo 'class="active"';?>
basename() strips the path from the filename, so you only have the pure file name
in_array() then checks if this path is in an array
array() generates an array of strings to be handed over to in_array(). Note that there are 4 separate strings, not one long one as in your code.
Use in_array for this purpose:
if (in_array($_SERVER['PHP_SELF'], array('faq.php', 'faq1.php', 'faq2.php', 'faq3.php')) echo 'class="active"';
http://php.net/manual/de/function.in-array.php
If you don't have any other pages that contain faq, you can simply use:
if (strpos($_SERVER['PHP_SELF'], 'faq') !== false)
Note that I am using !== false as strpos can return 0 when faq is found at the beginning of your string. You should probably use that for your other comparisons too.
Otherwise, go with the in_array solution of #MrTweek.

highlight current page link in opencart or any php

Im just learning open cart, but I think this question could be answered by anyone with good php knowledge.
I am simply attempting to highlight the a link when only on that page, but it doesnt seem to work
<?php $tickets = 'index.php?route=product/category&path=600'; ?>
<ul>
<li>Limerick FC</li>
<li><a href="<?php echo $tickets; ?>" <?php
if (strpos($_SERVER['PHP_SELF'], $tickets )) echo "class=\"current\" ";
?> > Tickets </a></li>
<li>Shop</li>
</ul>
I know the variable $tickets is fine because the link goes to where its supposed to go, and I know the class current is fine because it works for the third li which is shop.
Am I using strpos correctly?
I think you should use basename($_SERVER['REQUEST_URI']);.
<a href="<?php echo $tickets; ?>" <?php echo (basename($_SERVER['REQUEST_URI']) == $tickets) ? '"class=\"current\"' : ""; ?>>Tickets</a>
it would be more better if you declare basename($_SERVER['REQUEST_URI']) in variable.
strpos() returns false if the string matches at char 0 - which the above does.
Use indentical comparisons === for a true outcome.
As a note, i've used substr_count() in the past for similar circumstances without needing identical comparison!

Add Multiple Instances

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>

Adding classes with PHP echo, not replacing them

First of all, I know this is simple but I'm not a PHP developer by trade so I apologize if this is dumb for everyone. That being said, I've spent the last 3 hrs searching and can't find a way to do this without re-inventing my wheel so to speak. I use a simple command to mark the navigation on websites when the page is "current".
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'page')) echo 'class="current"';?> href="page.php" class="tp">Page</a></li>
Works great in most cases. However in this case, I need to concatenate the class string, not overwrite it with current. I've tried
...echo'class=" "."current".'
and several variations and can't get it to simply add the class not overwrite it. Thank you
Is the <li> not in a loop?
Ideally your menu would come from a table array through which you would loop. If that is the case; try this:
foreach(..){
$class = strpos($_SERVER['PHP_SELF'], 'page') ? " current" : "";
echo "<li>Page</li>";
}
Overall, for readability, I would recommend to not put if statements inline in the HTML.
But based on your code:
<?php
$class = strpos($_SERVER['PHP_SELF'], 'page') ? " current" : "";
?>
<li>Page</li>
What about something like this :
<li>
<a href="page.php"
class="<?php if (strpos($_SERVER['PHP_SELF'], 'page')) {echo 'current ';} ?>tp">
Page
</a>
</li>
This way, you'll always have at least class="tp", and, if the condition is met, a 'current ' (note the space at the end of this string : it's important and required so the two classes are separated by a space and not considered as one) will be inserted just before the 'tp', inside the class="..."

Categories