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

Superglobals?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Superglobals?

Postby BigZ1981 » Sat Jul 19, 2014 11:34 pm

I'm still a novice with PHP, so I'm not too familiar with how to use the superglobals. I'm trying to code a menu that will change the CSS class of each li element when it's the active page. I thought the code below would work for my purposes, but apparently not. Where am I going wrong?

Code: Select all
<?php
function menustyle($page) {
   if ($_SERVER["PHP_SELF"] == $page) {
      echo "class=amenuli";
   }
}
?>

<ul class="menu">
    <li><a href="index.php" <?php menustyle("index.php");?>>Home</a></li>
    <li><a href="techsvcs.php" <?php menustyle("techsvcs.php");?>>Technical Services</a></li>
    <li><a href="dessvcs.php" <?php menustyle("dessvcs.php");?>>Design Services</a></li>
    <li><a href="samples.php" <?php menustyle("samples.php");?>>Samples</a></li>
    <li><a href="aboutus.php" <?php menustyle("aboutus.php");?>>About Z-Tec</a></li>
    <li><a href="links.php" <?php menustyle("links.php");?>>Additional Resources</a></li>
    <li><a href="contactus.php" <?php menustyle("contactus.php");?>>Contact Us</a></li>
</ul>
BigZ1981
New php-forum User
New php-forum User
 
Posts: 7
Joined: Sat Jul 19, 2014 11:24 pm

Re: Superglobals?

Postby Strider64 » Sun Jul 20, 2014 3:24 am

Now I don't know if this is going to work, for I didn't test it. I used a ternary operator instead of an if statement, for it helps me visualize it better. Actually, you were quite close in your script....I think.

Code: Select all
<?php
function menustyle($page) {
   echo ($_SERVER['PHP_SELF'] == $page ) ? 'class="amenuli"' : NULL;
}
?>

<ul class="menu">
      <li><a href="index.php" <?php menustyle("index.php"); ?>>Home</a></li>
      <li><a href="techsvcs.php" <?php menustyle("techsvcs.php");?>>Technical Services</a></li>
      <li><a href="dessvcs.php" <?php menustyle("dessvcs.php");?>>Design Services</a></li>
      <li><a href="samples.php" <?php menustyle("samples.php");?>>Samples</a></li>
      <li><a href="aboutus.php" <?php menustyle("aboutus.php");?>>About Z-Tec</a></li>
      <li><a href="links.php" <?php menustyle("links.php");?>>Additional Resources</a></li>
      <li><a href="contactus.php" <?php menustyle("contactus.php");?>>Contact Us</a></li>
</ul>


I just like to add you should avoid using Global Variables as much as possible, but in this case there isn't any kind of security issues or bad coding practice going on.
User avatar
Strider64
New php-forum User
New php-forum User
 
Posts: 69
Joined: Sat Mar 23, 2013 8:24 am

Re: Superglobals?

Postby Strider64 » Sun Jul 20, 2014 4:00 pm

If was fooling around and the solution I gave you above didn't work, but I have a working solution.

Here it is:

Code: Select all
<?php
function menustyle($page) {
   $path_parts = pathinfo($_SERVER['PHP_SELF']);
   $base_name = $path_parts['basename'];    
   echo( $base_name == $page ) ? 'class="amenuli"' : NULL;
}
?>
<!doctype html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Subselect</title>
  <style>
     ul li {
         width: 250px;
         height: 30px;
         line-height: 30px;
         border-bottom: 3px solid #fff;
         background-color: orange;         
         list-style: none;
         padding: 10px;
      }
      
      ul li a {
         cursor: pointer;
         font-family: Arial, Helvetica, sans-serif;
         font-size: 1.2rem;
         line-height: 1.5;
         color: #fff;
         text-decoration: none;
      }
      
      ul li a:hover { color: green; }
      
       .amenuli {       
         color: green;
      }
  </style> 
</head>
<body>
<ul class="menu">
      <li><a href="index.php" <?php menustyle("index.php"); ?>>Home</a></li>
      <li><a href="techsvcs.php" <?php menustyle("techsvcs.php");?>>Technical Services</a></li>
      <li><a href="dessvcs.php" <?php menustyle("dessvcs.php");?>>Design Services</a></li>
      <li><a href="samples.php" <?php menustyle("samples.php");?>>Samples</a></li>
      <li><a href="aboutus.php" <?php menustyle("aboutus.php");?>>About Z-Tec</a></li>
      <li><a href="links.php" <?php menustyle("links.php");?>>Additional Resources</a></li>
      <li><a href="contactus.php" <?php menustyle("contactus.php");?>>Contact Us</a></li>
</ul>   
</body>
</html>


The Function is the only thing that matters, the rest of the code/html was done for testing purposes only.
User avatar
Strider64
New php-forum User
New php-forum User
 
Posts: 69
Joined: Sat Mar 23, 2013 8:24 am

Re: Superglobals?

Postby BigZ1981 » Sun Jul 20, 2014 9:19 pm

I could have sworn I posted a reply, maybe it didn't take. You're right, your solution didn't work, but I did figure out why. I ran a test page & echoed the value for $_SERVER['PHP_SELF'] and found that it included the leading /. All I did was add it to each one of my path values & it worked like a charm. ^_^
BigZ1981
New php-forum User
New php-forum User
 
Posts: 7
Joined: Sat Jul 19, 2014 11:24 pm

Re: Superglobals?

Postby Strider64 » Mon Jul 21, 2014 3:22 am

BigZ1981 wrote:I could have sworn I posted a reply, maybe it didn't take. You're right, your solution didn't work, but I did figure out why. I ran a test page & echoed the value for $_SERVER['PHP_SELF'] and found that it included the leading /. All I did was add it to each one of my path values & it worked like a charm. ^_^

Glad you got it working. Just a note $_SERVER['PHP_SELF'] will give the whole path and file name,

Code: Select all
   $path_parts = pathinfo($_SERVER['PHP_SELF']);
   $base_name = $path_parts['basename'];   


Doing something like the above I think would 99.9% ensure it to function ( no pun intended :D ) correctly.
User avatar
Strider64
New php-forum User
New php-forum User
 
Posts: 69
Joined: Sat Mar 23, 2013 8:24 am


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 3 guests

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