[英]PHP give active class to active item in navigation
I'm trying to give the active nav item a .active class using php but i don't know any solution to make this happen. 我正在尝试使用php为活动的nav项目提供.active类,但我不知道任何解决方案来实现这一目标。 At the moment its always number 1 active even if its not. 目前,它始终为1,即使没有激活。 I don't really see the problem so i was hoping you guys might can help me out. 我真的没看到问题,所以我希望你们能帮助我。 This is what i have at the moment: 这是我目前所拥有的:
<?php
$pages = $db->queryarray("
SELECT p.name,
u.url
FROM pages p
, url u
WHERE p.active=1
AND p.hidden=0
AND p.root=0
AND p.menu=1
AND u.type='s'
AND u.source=p.id
ORDER BY p.position ASC
");
$i=1;
foreach($pages as $page)
{
echo '<li>
<a title="Pagename"'.($i==1?' class="active"':"").'href="index.php#page_'.str_replace(' ','_',strtolower($page['name'])).'">'.$page['name'].'</a>
</li>';
$i++;
}
?>
@mhakvoort @mhakvoort
echo '<li><a title="Pagename" class="'.($page['active']) ? "active" : "" .'" href="index.php#page_'.str_replace(' ','_',strtolower($page['name'])).'">'.$page['name'].'</a></li>';
The first will always be true because of your if statement $i == 1;
由于您的if语句$i == 1;
第一个始终为真$i == 1;
To add the class active to your active page you would need something like this, depending on your field names.: 要将活动类添加到活动页面,您需要根据字段名称进行如下操作:
In your query you need to add the active boolean, and check on it in the a tag: 在查询中,您需要添加活动的布尔值,并在a标记中进行检查:
<?php
$pages = $db->queryarray("
SELECT p.name,
p.active,
u.url
FROM pages p
, url u
WHERE p.active=1
AND p.hidden=0
AND p.root=0
AND p.menu=1
AND u.type='s'
AND u.source=p.id
ORDER BY p.position ASC
");
$i=1;
foreach($pages as $page)
{
echo '<li>
<a title="Pagename"'.($page['active']) ? 'class="active"' : "").'href="index.php#page_'.str_replace(' ','_',strtolower($page['name'])).'">'.$page['name'].'</a>
</li>';
$i++;
}
?>
You need to have an identifier which page is the current page therefore we can take advantage of $_GET
php variable. 您需要有一个标识符,该页面是当前页面,因此我们可以利用$_GET
php变量。
Please note i have change index.php#page_
to index.php?page=
请注意,我已将index.php#page_
更改为index.php?page=
foreach ($pages as $page) {
$pageIdentifier = str_replace(' ','_',strtolower($page['name']));
$findActivePage = isset($_GET['page']) && $_GET['page'] == $pageIdentifier? " class='active' ": "";
echo '<li><a title="Pagename" '.$findActivePage.' href="index.php?page='.$pageIdentifier.'">'.$page['name'].'</a></li>';
}
You can also use jquery for adding active class when your anchor tag is click.Note I have added a menu_item
class to your anchor tag. 你也可以使用jQuery添加活动类时,你的锚标签click.Note我添加了一个menu_item
类来你的锚标记。
foreach ($pages as $page) {
$pageIdentifier = str_replace(' ','_',strtolower($page['name']));
echo '<li><a class="menu_item" id="page_'.$pageIdentifier.'" title="Pagename" href="index.php#page_'.$pageIdentifier.'">'.$page['name'].'</a></li>';
}
Jquery code jQuery代码
//menu item click event.
$("a.menu_item").on("click",function(){
//Remove active class in all a.menu_item
$("a.menu_item").removeClass("active");
//Add active class to the clicked menu_item
$(this).addClass("active");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.