这里介绍PHP的判断方法,实际应用中应考虑客户端运行环境,如果能用js,尽量用js实现。
操作IsURLCurrentPage()将判断按钮URL是否指向当前页面。这里使用strpos(),它可以查看给定的URL是否包含在服务器设置的变量中。strpos($_SERVER['PHP_SELF'], $url )语句将返回一个数字(如果$url中的字符串包含在全局变量$_SERVER['PHP_SELF'])或者false(如没有包含在全局变量中)。
<?php
class Page
{
//class Page's attributes
public $buttons = array("首页" => "index.php",
"虚拟主机" => "plans.php",
"合作加盟" => "aff.php",
"域名注册" => "domains.php",
"常见问题" => "faq.php",
"关于我们" => "about.php",
"客户中心" => "#"
);
//class Page's operations
public function DisplayMent($buttons)
{
echo "<ul class=\"nav nav-pills pull-right\">\n";
while (list($name, $url) = each($buttons)) {
$this->DisplayButton($name, $url,
!$this->IsURLCurrentPage($url));
}
echo "</ul>\n";
}
public function IsURLCurrentPage($url)
{
if(strpos($_SERVER['PHP_SELF'], $url )==false) {
return false;
} else {
return true;
}
}
public function DisplayButton($name, $url, $active = true)
{
if ($active) {
echo "<li><a href=\"".$url."\">".$name."</a></li>\n";
} else {
echo "<li class=\"active\"><a href=\"#\">".$name."</a></li>\n";
}
}
}
?>