如何添加主动与PHP的标签和标签丽 [英] how to add active to a tag and li tag with php

查看:122
本文介绍了如何添加主动与PHP的标签和标签丽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图让我的PHP的导航栏工作。我想呼应,用户是与附加类的积极的,如果页面是当前页。

下面是我的code。它显示的链接,我可以浏览网站,但它忽略了活动类。哈弗工程和链接样式。它只是忽略了活动类。

 < PHP
$页=阵列(
    的index.php=> 家,
    网页/ contact.php=> 联系我们,
    页/ services.php=> 服务,
    页/ employees.php=> 雇员,
    页/ dashboard.php=> 仪表板);的foreach($页为$ URL => $标签){
  呼应'<李';  如果(使用isset($ _ GET ['页'])及和放大器; $ _GET [页] == $网址){
    回声'级=活跃';
  }
  回声'>&下; A HREF =,$网址,>',$标签,&所述; / A>&下; /利>';
}
?>


解决方案

 < PHP
$页=阵列(
    的index.php=> 家,
    网页/ contact.php=> 联系我们,
    页/ services.php=> 服务,
    页/ employees.php=> 雇员,
    页/ dashboard.php=> 仪表板);$ cururl = $ _ SERVER ['PHP_SELF'];的foreach($页为$ URL => $标签){
  呼应'<李';  如果(strpos($ cururl,$网址)!== FALSE){
    回声级=活动;
  }
  回声'>&下; A HREF =,$网址,>',$标签,&所述; / A>&下; /利>';
}
?>

这应该为你工作。因为,你试图通过访问URL地址 GET法。在这里,我已经做取出你的网址 $ cururl = $ _ SERVER ['PHP_SELF']; 和检查活动的菜单项为如果(strpos ($ cururl,$网址)!== FALSE)

Trying to get my PHP navigation bar to work. I am trying to echo the page where the user is on with the attached class of active if the page is current.

Here is my code. It shows the links and I can navigate the site but it ignores the active class. Hover works and links are styled. It just ignores the active class.

<?php
$pages = array(
    "index.php" => "Home", 
    "pages/contact.php" => "Contact Us", 
    "pages/services.php" => "services", 
    "pages/employees.php" => "Employees", 
    "pages/dashboard.php" => "Dashboard");

foreach ($pages as $url => $label) {
  echo '<li ';

  if (isset($_GET['page']) && $_GET['page'] == $url) {
    echo '"class=active"';
  }
  echo '><a href=', "$url", '>', "$label", '</a></li>';
}
?>

解决方案

<?php
$pages = array(
    "index.php" => "Home", 
    "pages/contact.php" => "Contact Us", 
    "pages/services.php" => "services", 
    "pages/employees.php" => "Employees", 
    "pages/dashboard.php" => "Dashboard");

$cururl = $_SERVER['PHP_SELF'];

foreach ($pages as $url => $label) {
  echo '<li';

  if (strpos($cururl,$url)!==false) {
    echo ' class="active"';
  }
  echo '><a href=', "$url", '>', "$label", '</a></li>';
}
?>

This should work for you. Because, you were trying to access the url address using GET-METHOD. Here, I have fetched your url by doing $cururl = $_SERVER['PHP_SELF']; and checked for the active menu item as if (strpos($cururl,$url)!==false)

这篇关于如何添加主动与PHP的标签和标签丽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆