PHP的情况下,如果当前页面,然后链接高亮显示 [英] PHP condition, if current page, then link is highlighted

查看:106
本文介绍了PHP的情况下,如果当前页面,然后链接高亮显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用一些CSS类侧边栏的网页。如果类=积极的,那么该链接将被高亮显示。我希望让这个在任何网页时,脚本会检查该链接它在,然后再决定要强调了链接。下面是我在哪里,但我真的没有线索如何,请点我在正确的和更好的方向!如果我有20个链接,这个脚本会太长....谢谢!


 < PHP
$ ARR =阵列('1'=> /about.php',
'2'= GT; /abt-history2.php',
'3'= GT; /abt-shop.php',
);$主机= $ _ SERVER ['REQUEST_URI'];
如果($主机== $改编['1'])
{
    $ 1级=主动;
}否则,如果($主机== $改编['2'])
{
    $类class2 =主动;
}否则,如果($主机== $改编['3'])
{
    $ 3类=主动;
}
 ?>
   &所述;微升的id =navigationSide>
      < D​​IV的风格=填充左:20像素;填充底:10px的; FONT-SIZE:16px的;字体重量:800;颜色:#777;> TITLE< / DIV>
      <立GT;< A HREF =< PHP打印$改编['1'];?>中CLASS =<?PHP的回声$ 1级>> AA< / A>< /李>
      <立GT;< A HREF =< PHP打印$改编['2'];?>中CLASS =<?PHP的回声$类class2>> BB< / A>< /李>
    < / UL>
    < D​​IV的风格=填充:为5px;>< / DIV>
    &所述;微升的id =navigationSide>
      < D​​IV的风格=填充左:20像素;填充底:10px的; FONT-SIZE:16px的;字体重量:800;颜色:#777;>&TITLE2 LT; / DIV>
      <立GT;< A HREF =< PHP打印$改编['3'];?>中CLASS =<?PHP的回声$ 3类>? > CC< / A>< /李>< / UL>


解决方案

好吧,这是新的,我删除了所有我以前贴过,这是另一种答案。
一直以来, $ _ SERVER ['REQUEST_URI'] 获得 /Portal/TEST/file.php 在我的本地主机,我有加入 /门户/测试/ 在脚本中,你可以根据你的文件的结构将其删除。

 < PHP
$ ARR =阵列(0=> /Portal/TEST/about.php',
'1'=> /Portal/TEST/abt-history2.php',
'2'= GT; /Portal/TEST/abt-shop.php',
'3'= GT; /Portal/TEST/extra.php',
'3'= GT; /Portal/TEST/blaze.php',
);
 ?>
< H2> TITLE< / H>< PHP 为($ I = 0; $ I<计数($ ARR); $ I ++){    如果($ _ SERVER ['REQUEST_URI'] == $改编[$ i]){    $类=主动;
 ?>
  <立GT;< A HREF =< PHP打印$改编[$ i];>中CLASS =<?PHP的回声$类$ I方式>?><?PHP的echo $类>
< / A>< /李> < PHP}其他{
$类='在活动';?>
  <立GT;< A HREF =< PHP打印$改编[$ i];>中CLASS =<?PHP的回声$类$ I方式>?><?PHP的echo $类>
< / A>< /李>< PHP
} ?}>

I have a webpage with a side bar that uses some css class. If the class = "active', then the link will be highlighted. I want to make it so that at any page, the script would check which link it's at, then decide which link to be highlighted. Below is where I'm at, but I really don't have a clue how to proceed, please point me in the right and better direction! If I have 20 links, this script would be way too long.... Thank you!


<?php






$arr = array(

'1' => '/about.php', 
'2'=> '/abt-history2.php',
'3' => '/abt-shop.php',
);

$host =  $_SERVER['REQUEST_URI'];


if($host == $arr['1']) 
{
    $class1 = "active";
}

else if ($host == $arr['2']) 
{
    $class2 = "active";
}

else if ($host == $arr['3']) 
{
    $class3 = "active";
}


 ?>






   <ul id="navigationSide">
      <div style="padding-left:20px; padding-bottom:10px; font-size:16px; font-weight:800; color:#777;">TITLE</div>
      <li><a href="<?php print $arr['1'];  ?>" class="<?php echo"$class1" ?>">AA</a></li>
      <li><a href="<?php print $arr['2'];  ?>" class="<?php echo"$class2" ?>">BB</a></li>
    </ul>
    <div style="padding:5px;"></div>
    <ul id="navigationSide">
      <div style="padding-left:20px; padding-bottom:10px;  font-size:16px; font-weight:800; color:#777;">TITLE2</div>
      <li><a href="<?php print $arr['3'];  ?>" class="<?php echo"$class3" ?>" >CC</a></li>

</ul>

解决方案

Ok, this is new, I removed all I had posted before, and this is what another answer. Since, $_SERVER['REQUEST_URI'] gets /Portal/TEST/file.php in my localhost, I have added /Portal/TEST/ in the script, you can remove it depending on the structure of your files.

<?php 
$arr = array(

'0' => '/Portal/TEST/about.php', 
'1'=> '/Portal/TEST/abt-history2.php',
'2' => '/Portal/TEST/abt-shop.php',
'3' => '/Portal/TEST/extra.php',
'3' => '/Portal/TEST/blaze.php',
);




 ?>


<h2>TITLE</h2>

<?php 

 for($i=0; $i< count($arr); $i++) {  



    if($_SERVER['REQUEST_URI'] == $arr[$i]) {

    $class = 'active';
 ?>


  <li><a href="<?php print $arr[$i];  ?>" class="<?php echo "$class".$i ?>"><?php echo $class ?>
</a></li>   <?php }else {
$class  = 'In active';

?>  
  <li><a href="<?php print $arr[$i];  ?>" class="<?php echo "$class".$i ?>"><?php echo $class ?>
</a></li> 

<?php
}

 }?>

这篇关于PHP的情况下,如果当前页面,然后链接高亮显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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