Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

php - add active class to li from page url

I am using the following to get the active page url:

<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
?>

echoing the above gives me my URL as follows:

http://www.domain-nane.com/index.php?option=com_content&view=article&id=6&Itemid=109

I want to add a class of 'current' to my 'li' if the active page is present - dependant on the 'Itemid' contained in the url - i.e.

<li class="current">
   <a href="index.php?option=com_content&amp;view=article&amp;id=6&amp;Itemid=109">Link 1</a>
<li>
   <a href="index.php?option=com_content&amp;view=article&amp;id=6&amp;Itemid=110">Link 2</a>
</li>
<li>
   <a href="index.php?option=com_content&amp;view=article&amp;id=6&amp;Itemid=111">Link 3</a>
</li>

Any hints/ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you are doing wrong thing

simple way is below..

In your URL Itemid is unique, so its better to do this in following way

<li <?php if($Itemid == $_REQUEST['Itemid']) { echo class='current'; } ?> >
   <a href="index.php?option=com_content&amp;view=article&amp;id=6&amp;Itemid=109">Link 1</a>
</li>

<li <?php if($Itemid == $_REQUEST['Itemid']) { echo class='current'; } ?> >
   <a href="index.php?option=com_content&amp;view=article&amp;id=6&amp;Itemid=110">Link 2</a>
</li>

<li <?php if($Itemid == $_REQUEST['Itemid']) { echo class='current'; } ?> >
   <a href="index.php?option=com_content&amp;view=article&amp;id=6&amp;Itemid=111">Link 3</a>
</li>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...