I have a situation here. In my project the menu section contains some anchor tags, it will work perfectly when we in index page, but moving to other pages i want to give the real links there, so my question is how to check which page is viewing or how to check the the site viewer is not in index page
<li><a href="#home" class="active">Home</a></li>
<li><a href="#about_us">About Us</a></li>
I want to change href
conditionally, for example when I'm in index the above href
attribute is OK, and when I'm in another page, for example register, then the href
attribute change to index.php/site/index#home
Thanks in advance
UPDATE : thank you uttara,I found a solution with the help of her
<?php
$url = $_SERVER["REQUEST_URI"];
$page = pathinfo($url);
$filename = $page['filename'];
$href = ($filename=='root_directory' || $filename=='index' || $filename=='site')?'':Yii::app()->request->baseUrl;
?>
<a href="<?php echo $href ; ?>#home" class="active">Home</a>
$url = $_SERVER["REQUEST_URI"];
$page = pathinfo($url);
$filename = $page['filename'];
$filename
will give you the name of current page being viewed
and you can check for
if($filename != 'index')
{
echo '<li><a href="index.php/site/index#home" class="active">Home</a></li>
<li><a href="#about_us">About Us</a></li>';
}
else
{
echo '<li><a href="#home" class="active">Home</a></li>
<li><a href="#about_us">About Us</a></li>';
}
remember $filename
gives you just the filename without extension
You can use the CMenu widget, and this takes care of the highlights, appearance, etc. Plus you can use custom CSS afterwards.
<?php
$this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=> 'YOUR_URL#home'),
array('label'=>'About Us', 'url'=>'YOUR_URL#about_us)'
)
);
?>
first give the id to anchor tag and then put condition like below
<?php
// the php code
$flag=strpos('index.php',$_SERVER['PHP_SELF'])
{
?>
<script>
$(document).ready(function(){
$("#idofanchortg").attr('href','hrefyou want to add');
});
</script>
<?php } ?>
you can use CMenu widget provided by Yii http://www.yiiframework.com/wiki/211/creating-a-css-driven-drop-down-menu-using-cmenu/
First of all I prefer to leave responsibility in just one place.
<li><a href="<?php echo $this->getHomeHrefLink(); ?>" class="active">Home</a></li>
<li><a href="#about_us">About Us</a></li>
I prefer this way 'couse code is more clean. Well. Now we know that all our controllers extends Controller class (/protected/components/Controller.php). In this place we can add
public function getHomeHrefLink() {
// when I'm in index page the href attribute is #home
// when I'm in register page the href attribute is index.php/site/index#home
}
So come on: We can you $this->action (for controller name) and $this->action->id (for action name) to understand where we are:
public function getHomeHrefLink() {
return $this->createUrl('index/site', array());
// when I'm in register page the href attribute is index.php/site/index#home
return '#home;
}