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"';}?>
Related
I am having problems pulling the pages through in PHP and HTML I have used :-
<li>
<!-- Pulling Categories from the database
dynamically -->
<?php
$nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject =
mysqli_fetch_assoc($nav_subjects)) {
?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
Which pulls the categories dynamically from the database and displays them with a drop down arrow just how I wanted but the pages will not show underneath them here's the code I have used for that bit:-
<!-- Categories listed correctly let's pull the pages for each one -->
<?php
if($nav_subject['id'] == $subject_id) {
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) {
?>
<ul>
<li>
<?php echo h($nav_page['menu_name']); ?>
</li>
</ul>
<?php } // while $nav_pages
} // if($nav_subject['id'] == $subject_id)
} // while $nav_subjects ?>
</li>
<?php
mysqli_free_result($nav_subjects);
mysqli_free_result($nav_pages);
?>
I am pulling in the SQL from another page which is loaded correctly as the categories load and display correctly.
I will be grateful for any ideas.
I have also tried to echo back the sql result but nothing is shown.
I have now got it working with the following code:-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject = mysqli_fetch_assoc($nav_subjects)) {?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<li><?php echo h($nav_page['menu_name']); ?></li>
<?php } ?>
<?php } ?>
</ul>
But now it is listing the secondary subjects as a child of the first instead of individual subjects of their own.
*********Resolved**********
please advise if you think the code could be better i've currently used :-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);?>
<?php while($nav_subject = mysqli_fetch_assoc($nav_subjects)){?>
<span class="opener"><?php echo h($nav_subject['menu_name']);?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<a href="<?php echo url_for('index.php?id=' . h(u($nav_page['id']))); ?>">
<?php echo h($nav_page['menu_name']); ?></a>
<?php } ?>
<?php mysqli_free_result($nav_pages); ?>
</ul>
<?php } ?>
<?php mysqli_free_result($nav_subjects); ?>
</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 need the "Pages" link to be able to detect if the user is on any of the pages that are included in the dropdown menu for "Pages". Detecting whether they are on only 1 page is easy, I need to add multiple instances to the "Pages" item. Hopefully that makes a little more sense.
Here is what I have:
<li class='dropdown' id="menu1">
<a class="dropdown-toggle" data-toggle="dropdown" href="#menu1">Pages</a>
<ul class="dropdown-menu">
<li>
Home
</li>
<li>
About
</li>
</ul>
</li>
A example of what the code would look like for JUST index.php would be:
<li <?php if (stripos($_SERVER['REQUEST_URI'],'index.php') !== false) {echo 'class="active"';} ?>>
Home
</li>
Like I said before, I need to add multiple instances in basically one PHP block to detect MORE THAN if the user is JUST ON [filename].php
Hopefully this makes sense.
First you need to extract just the page name, then you need to compare it to the array of posible pages:
<?php if (in_array(preg_replace('|.*/(.+\.php).*|i','\\1',$_SERVER['REQUEST_URI']), array('index.php','about.php'))) {echo 'class="active"';}
You'll have to put your list items in an array. And then loop over them to create the menu.
$pages = array(
'Home' => 'home.php',
'About' => 'about.php'
);
<?php foreach ($pages as $name => $url): ?>
<li <?php if (stripos($_SERVER['REQUEST_URI'], $url) !== false):
echo 'class="active"';
endif ?>>
<?php echo $name?>
</li>
<?php endforeach ?>
This will check each result in the array to see if it matches the uri. If it does it will add the active class to the li.
I have a magento website with multi store and i have been able to add the home link on the default store. the whole site along with its multi stores shares the same theme but in the theme i edited template/page/navigation/top.php with the code
<?php
$_anyActive = false;
foreach ($this->getStoreCategories() as $_category)
{
$_anyActive = $_anyActive || $this->isCategoryActive($_category);
}
?>
<li class="home <?php echo !$_anyActive ? 'active' : '' ?>">
<span><?php echo $this->__('Home') ?></span>
</li>
This code now makes the home link shows but now its only shows in the default store but i want it to show in all other stores, i dont understand this but as they whole stores shares the same theme, i thought maybe they should also be able to read this code and display the home link, i could duplicate the themes and then assign them to each store (which i'm not even sure would work) but that would make the code deficult to maintain as i though if they share the same design, i could just make one change and reflect on the entire sub stores.
The following is the content of template/page/navigation/top.php
<?php $_menu = ''?>
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php $_menu .= $this->drawItem($_category) ?>
<?php endforeach ?>
<?php if ($_menu): ?>
<div class="nav-container">
<ul id="nav">
<?php $_anyActive = false; foreach ($this->getStoreCategories() as $_category) { $_anyActive = $_anyActive || $this->isCategoryActive($_category); } ?>
<li class="home <?php echo !$_anyActive ? 'active' : '' ?>"><span><?php echo $this->__('Home') ?></span></li>
<?php echo $_menu; ?>
</ul>
</div>
<?php endif; ?>
I hope you are able to help me solve this problem
Use following code
<li class="home <?php if (Mage::helper('core/url')->getCurrentUrl() === Mage::helper('core/url')->getHomeUrl()):?> active<?php endif;?>"><span><?php echo $this->__('Home') ?></span></li>
I guess the problem is that you put your home link inside of main navigation. As your secondary stores don't have any navigation, the whole unordered list has not been generated.
Try this:
<?php
$_menu = '';
$_anyActive = false;
foreach ($this->getStoreCategories() as $_category){
$_menu .= $this->drawItem($_category);
$_anyActive = $_anyActive || $this->isCategoryActive($_category);
}
?>
<div class="nav-container">
<ul id="nav">
<li class="home <?php echo !$_anyActive ? 'active' : '' ?>"><span><?php echo $this->__('Home') ?></span></li>
<?php echo $_menu; ?>
</ul>
</div>
Neeraj Garg's answer was the solution in the end, but I wanted to expand on it.
Go to /app/design/frontend/default/yourtheme/template/page/html/topmenu.phtml
That might be specific to my theme. I think most would be using top.phtml in a different folder, as mentioned in the question. Turn on Template Path Hints in the System->Configuration->Advanced->Developer section (you may need to change your scope) to find out what file your navigation menu can be edited in. Make sure you copy and paste it outside of your base theme into your current theme, if it is using the base theme.
After that, I used a similar solution to Neeraj's suggestion. Mine looks like this, and is obviously dependent on the theme I am using (yours will likely look much different, as it will likely need to be catered to your theme).
<li class="level0 nav-0 first level-top <?php if (Mage::helper('core/url')->getCurrentUrl() === Mage::helper('core/url')->getHomeUrl()):?> active<?php endif;?>">
<a href="\" class="level-top">
<span>Home</span>
</a>
</li>
<?php echo $_menu ?>
So I have a menu in a php file that looks like this (This is the whole file. I'm totally new to PHP.)
menu.php:
<li id="current"><span>Home</span></li>
<li><span>Blog</span></li>
<li><span>Results</span></li>
<li><span>Pictures</span></li>
<li><span>Our Location</span></li>
Now in my pages I do this (index.php):
<div id="tabs1" >
<ul>
<!-- CSS Tabs -->
<?php include("menu.php"); ?>
</ul>
</div>
So what I want to be able to do is change the line above to this:
<?php include("menu.php?current=pictures"); ?>
Which would make the active tab the Pictures tab. How can I do this?
You could also try this:
Your php script
<?php
$selected = "pictures";
$current_id = ' id="current"';
include "menu.php";
?>
this is your menu:
<ul>
<li <?php if ($selected == "pictures") print $current_id; ?>><span>Home</span></li>
<li <?php if ($selected == "blog") print $current_id; ?>><span>Blog</span></li>
<li <?php if ($selected == "home") print $current_id; ?>><span>Results</span></li>
<li <?php if ($selected == "me") print $current_id; ?>><span>Pictures</span></li>
<li <?php if ($selected == "contacts") print $current_id; ?>><span>Our Location</span></li>
</ul>
Try this:
<li <?php if($_GET['current'] == 'home') {echo 'id="current"'}?>><span>Home</span></li>
<li <?php if($_GET['current'] == 'blog') {echo 'id="current"'}?>><span>Blog</span></li>
<li <?php if($_GET['current'] == 'results') {echo 'id="current"'}?>><span>Results</span></li></li>
and so on....
worth looking at
intelligent navigation
<nav>
<style>
#active{
color:#FFC801;
}
</style>
<?php
$activePage = basename($_SERVER['PHP_SELF'], ".php");
?>
<ul>
<li>About Us</li>
<li>Mentors</li>
<li>Tours</li>
<li>Animation</li>
<li>Blog</li>
<li>Testimonials</li>
<li>Press/Media</li>
<li>Facts</li>
</ul>
</nav>
I don't think its necessary for it to be done at the server side (using up CPU cycles).
Use javascript/CSS to achieve this.