Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Checking page name to change links class

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Checking page name to change links class

Postby MikeSpider » Wed Nov 21, 2012 7:45 am

Hi guys,
I wrote a script to indicate the active page by changing the element class.

But it's not working, need some help. Here is the script:

Code: Select all


                    <?php  $menuItems = array('home', 'create','projects','forums', 'connections','settings','login','donate');
                   
                    for($i=0; $i< count($menuItems); $i++){
                           if($_SERVER['PHP_SELF'] == $menuItems[$i].".php"){
                               $active = "navBlockActive";
                           }else{
                               $active = "navBlock";
                           }                                   
                             echo '<a href="'.$menuItems[$i].'.php">
                             <div class="'.$active.'">';
                        echo '<p>'.$menuItems[$i] . '</span>';
                        echo '</div></a>';
                        } ?>


Thanks in Advance,
Mike
MikeSpider
New php-forum User
New php-forum User
 
Posts: 49
Joined: Sun Feb 05, 2012 12:48 pm

Re: Checking page name to change links class

Postby us2rn4m2 » Wed Nov 21, 2012 8:26 am

Hi,
(Sorry for my English)

Path is missing in this line:
Code: Select all
if($_SERVER['PHP_SELF'] == $menuItems[$i].".php")

echo ($_SERVER['PHP_SELF']) == "/path/" . $menuItems[$i] . ".php"
us2rn4m2
New php-forum User
New php-forum User
 
Posts: 17
Joined: Thu Aug 09, 2012 1:28 pm

Re: Checking page name to change links class

Postby MikeSpider » Wed Nov 21, 2012 9:04 am

Hi,
thanks for your reply.

I tried :
Code: Select all
$_SERVER['PHP_SELF']) == "localhost/Mypage/" . $menuItems[$i] . ".php"


not working, also tried: Mypage/. $menuItems[$i] . ".php"
and "../".$menuItems[$i].'.php')
not working either.

thanks,
Mike
MikeSpider
New php-forum User
New php-forum User
 
Posts: 49
Joined: Sun Feb 05, 2012 12:48 pm

Re: Checking page name to change links class

Postby us2rn4m2 » Wed Nov 21, 2012 9:16 am

Yeah but your code works for me, so ?

$_SERVER['PHP_SELF'] value must be exactly the same as "/path/" . $menuItems[$i] . ".php"
This is my $_SERVER['PHP_SELF'] value: "/projects/php/index.php" and yours ?

PHP manual say:
"PHP_SELF
The filename of the currently executing script, relative to the document root."


This code works for me !
Code: Select all
<?php $menuItems = array('home', 'create','projects','forums', 'connections','settings','login','donate');

for($i=0; $i< count($menuItems); $i++) {
      if($_SERVER['PHP_SELF'] == "/projects/php/index.php") {
         $active = "navBlockActive";
      }else{
         $active = "navBlock";
      }                                   
       echo '<a href="'.$menuItems[$i].'.php">
       <div class="'.$active.'">';
   echo '<p>'.$menuItems[$i] . '</span>';
   echo '</div></a>';
   } ?>
us2rn4m2
New php-forum User
New php-forum User
 
Posts: 17
Joined: Thu Aug 09, 2012 1:28 pm

Re: Checking page name to change links class

Postby MikeSpider » Wed Nov 21, 2012 9:44 am

Yes it worked.
so, one thing I understand now is that $_SERVER['PHP_SELF'], always refers to my htdocs folder as a root, and then the project name and then the page !?

I used as path: /myproject/

Thanks a lot for your help.

Mike
MikeSpider
New php-forum User
New php-forum User
 
Posts: 49
Joined: Sun Feb 05, 2012 12:48 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Baidu [Spider] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron