I have a small php website that has a menu that is included via include_once. I need an HTML class added to the current menu item for each page.
So if a user clicks on a link and goes to that page the new page will load with the new menu item with class="current". How can I go about doing this?
Let's say you write your menu like this:
<ul>
<li> Link1 </li>
<li> Link2 </li>
<li> Link3 </li>
</ul>
Here is a good old php4 style solution :-p
<?php
//you get which link was clicked
if(isset($_GET['menu']) $menu = $_GET['menu'];
else $menu = 1;
$1 = $2 = $3 = ""; //you create 3 empty strings
switch($menu){ //you assign the correct string with your css class name
case "1": $1 = 'class="current"';break;
case "2": $2 = 'class="current"';break;
case "3": $3 = 'class="current"';break;
}
?>
//echo the variables into the menu anchors ( <?=$1?> is equivalent to <? echo $1; ?> )
<ul>
<li><a href="link1.com?menu=1" <?=$1?> >Link1</a></li>
<li><a href="link2.com?menu=2" <?=$2?> >Link2</a></li>
<li><a href="link3.com?menu=3" <?=$3?> >Link3</a></li>
</ul>
You can also place the <?=$1?> into the <li> or anywhere else wanted...
If you're using javascript in your project, you could also give an id to your anchors (like Link1), get the menu value like above with $_GET, and then use javascript to add the class to the desired link.
I gave you this answer because from what you're writing I guess you're not using any PHP framework but coding you app the old way... there's plenty of nice PHP framework around here that will have pre-made solution for this... might be hard at the beginning but it's worth it...
Good luck!
Related
I want the user can switch languages without changing the current page by clicking the language link.
I found a code like the one below. It works but cannot find the page because the page names are different. For example: When I change the language while on "../en/about.php" page, it goes to "../tr/about.php" page. The page that should go is "../tr/hakkimizda.php".
How can I solve this problem?
<?php
//URL path. eg: index-en/job.php
$path = trim($_SERVER['REQUEST_URI'],'/');
//language from URL. eg: index-en
$lang = explode('/',$path)[0];
//Paths in other languages: eg: 'tr' => 'index-cn/job.php'
$langs = [
'en'=>preg_replace("/$lang/",'../en',$path,1),
'tr'=>preg_replace("/$lang/",'../tr',$path,1),
];
?>
<ul class="dropdown-menu">
<li>
Türkçe
</li>
<li>
English
</li>
</ul>
You can use global arrays for each language and search current path into the corresponding array when you should get the key of this path. Then you can use the array for another language to search the path for this key, for example, you have:
$EN = array('about' => 'about.php');
$TR = array('about' => 'hakkimizda.php');
FIRST:
search the key for 'about.php' in the array $EN;
THEN:
use this key (which would be about) to search corresponding entry in the $TR.
I suggest, that these arrays should be global
I don't know php language but I solved the problem with javascript.
Solution:
<a class="nav-link" href="#" id="LangRedirect">English</a>
JavaScript
$("#LangRedirect").click(function LangRedirect(){
var lang,en,tr;
lang=window.location.pathname;
en="/en/";
tr="/tr/";
switch (lang){
// About
case en+"about.php": location.assign(tr+"hakkimizda.php");
break;
case tr+"hakkimizda.php": location.assign(en+"about.php");
break;
default: window.location.pathname=(en+"404.php");
}});
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.
What i'm trying to do is quite simple as concept, but i'm not that good with php and the joomla framework.
Currently the home menu-item is generated like this:
<li class="item-101 current active">
<a class="hide-text" href="#some-link">Home</a>
</li>
What i'd like to achieve is to insert an <i> element inside only the home menu-item, something like this:
<li class="item-101 current active">
<a class="hide-text" href="#some-link"><i class="icon-home"></i>Home</a>
</li>
How can i achieve this? I'm using Joomla! 2.5 atm
I guess it's possible do something like "if this menu-item is the home link then add this code inside the <a> tags" but i really don't know how to do it, my php is not strong enough :P
Note: i'm doing this to achieve a simple home-icon instead of the litteral home menu-item.
As the classes syntax could suggest, i'm using the twitter bootstrap css-framework, but i've implemented the Icomoon font-set (as in joomla 3.0) instead of the tbs Glyph-icons sprites images.
Unfortunately, using font-based icons, text rules are applied to the icons also, and that is the reason i'm trying to insert a custom element inside the <a> tag, so that i can override the hide-text class hiding the home-icon.
Thanks for any suggestion!
Have a look in the template file of the menu module.
/modules/mod_menu/tmpl/default.php
The template builds the HTML for the menu module.
I just checked how it works in Joomla 2.5, and in the /modules/mod_menu/tmpl/default.php template the list is build. If you want to add to only the home link you'll have to add a little bit of code. Something like this :
if($item->home == '1'){ $item->title = '<i class="icon-home"></i>' . $item->title; };
Insert this just under the foreach loop and have a go, it should look something like this :
foreach ($list as $i => &$item) :
// THIS ADDS THE <i> to only the HOME LINK
if($item->home == '1'){ $item->title = '<i class="icon-home"></i>' . $item->title; };
$class = 'item-'.$item->id;
if ($item->id == $active_id) {
$class .= ' current';
}
Good Luck ;)
I have been following this guide as well, (Thanks Gruber and Mark Vink) but using the glyphicons instead of IcoMoon. I found there to be a syntax error in the example above. The version hat worked for me was
foreach ($list as $i => &$item) {
if($item->home == '1')$item->title = '<span class="glyphicon glyphicon-home" aria-hidden="true"></span>' .$item->title;
$class = 'item-' . $item->id
etc...
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="..."
I am new to CodeIgniter so I'm just trying to create a pretty basic site. I have 4 controllers/pages that I want to load, and a possibility to add a few more.
I have an array of items in my /applications/config/site.php file (which is autoloaded) as shown:
$site['MenuItems']['Home'] = "http://mysite.com/site/home";
$site['MenuItems']['Network Info'] = "http://mysite.com/site/info";
$site['MenuItems']['Staff'] = "http://mysite.com/site/staff";
$site['MenuItems']['Support'] = "http://mysite.com/site/support";
$config['site'] = $site;
I want to be able to take the $site['MenuItems'] array and echo out key/value pairs to place ultimately into my view page so that they are displayed as links on my site in the header. I want to be able to add and subtract items from this $site['MenuItems'] array as I need to to create more links in my header.
For example, in my view if I were to echo out the 'Home' => "http://mysite.com/site/home" key value pair:
<li>
Home
</li>
I'm not sure if I use $this->config->load('site','MenuItems') to do this...or what?
Thanks for any help you can provide me. Let me know if I'm missing something. It's probably something incredibly easy and I just can't grasp it right now :(
Controller's code:
$data['MyVarsArray'] = "That's my menu!";
$data['MyLinks'] = $this->config->item('site');
$this->load->view('myview',$data);
myview.php code:
<h2><?=$MyVarsArray?></h2>
<ul>
<?php
foreach($MyLinks['MenuItems'] as $key=>$value){?>
<li>
<?=$key?>
</li>
<?}
?>
</ul>
try this
Controller's code:
$data['MyVarsArray'] = "That's my menu!";
$data['MyLinks'] = $this->config->item('MenuItems');
$this->load->view('myview',$data);
myview.php code:
<h2><?=$MyVarsArray?></h2>
<ul>
<?php
foreach($MyLinks as $key=>$value){?>
<li>
<?=$key?>
</li>
<?}
?>
</ul>