Web Hosting Talk







View Full Version : Header with nav links in php


roberto-bbh
02-12-2010, 05:30 PM
Hi, I just changed my website from html pages to php due to some limitations html offers and now I have an annoying problem with my header navigation links.

I have an attribute for links, that is class="current" .It makes the button to be white on the current page you are, for example if I am on About page this attribute makes the button About white, selected, so I know where I am on the website.

This worked because each main page has its part o header written down with the class="current" set for it but now because I use a global header.php file with include() the thingy doesn't work anymore because there is no attribute set for each button depending on the page.

So is there a way to set the class="current" to the current page's link with php ?

The code for header is like this

div id="header-nav">
<ul>
<li><a href="index.php" class="current">Home</a></li>
<li><a href="hosting-plans.php" >Hosting Plans</a></li>
<li><a href="domains.php" >Domains</a></li>
<li><a href="about.php" >About</a></li>
<li><a href="contact.php">Contact</a></li>
</ul>
</div>

So with this code the Home button is "selected" because it has the attribute, but it remains the same because its a global included file.

How can I fix this ?

Thank You

mattle
02-12-2010, 09:23 PM
Hi, I just changed my website from html pages to php due to some limitations html offers and now I have an annoying problem with my header navigation links.

Sorry...gotta stop you here...I just love that line. HTML! Now with new limitations! :stickout:

I have an attribute for links, that is class="current" .It makes the button to be white on the current page you are, for example if I am on About page this attribute makes the button About white, selected, so I know where I am on the website.

This worked because each main page has its part o header written down with the class="current" set for it but now because I use a global header.php file with include() the thingy doesn't work anymore because there is no attribute set for each button depending on the page.

So is there a way to set the class="current" to the current page's link with php ?

The code for header is like this

div id="header-nav">
<ul>
<li><a href="index.php" class="current">Home</a></li>
<li><a href="hosting-plans.php" >Hosting Plans</a></li>
<li><a href="domains.php" >Domains</a></li>
<li><a href="about.php" >About</a></li>
<li><a href="contact.php">Contact</a></li>
</ul>
</div>So with this code the Home button is "selected" because it has the attribute, but it remains the same because its a global included file.

How can I fix this ?

Thank You

What you want is to have the include file dynamically choose which link to apply that class to. Associative arrays are great for this:


$links = array(
'index.php' => 'Index',
'hosting-plans.php' => 'Hosting Plans',
//...
);

foreach ($links as $url => $text)
{
$selected = ($_SERVER['PHP_SELF'] == $url ? 'class="current"' : '');
echo<<<HTML
<li><a href="$url" $selected>$text</a></li>
HTML;
}

roberto-bbh
02-13-2010, 02:12 PM
wow, thanks, now I just need to figure out how to put all this stuff in my page cause I don't seem to succeed.

<div id="header-nav">
<?php
$links = array(
'index.php' => 'Index',
'hosting-plans.php' => 'Hosting Plans',
//...
);

foreach ($links as $url => $text)
{
$selected = ($_SERVER['PHP_SELF'] == $url ? 'class="current"' : '');
echo'


<ul>
<li><a href="$url" $selected>$text</a></li>
</ul>
';
}?>

</div>
All the signs and combination with html is overwelming me

mattle
02-14-2010, 12:18 PM
CODE tags converted to PHP...

wow, thanks, now I just need to figure out how to put all this stuff in my page cause I don't seem to succeed.

<div id="header-nav">
<?php
$links = array(
'index.php' => 'Index',
'hosting-plans.php' => 'Hosting Plans',
//...
);


Okay...the "//..." was just a comment to indicate that you should put the rest of your links in the array in the same fashion as the two I provided.

foreach ($links as $url => $text)
{
$selected = ($_SERVER['PHP_SELF'] == $url ? 'class="current"' : '');
echo'


<ul>
<li><a href="$url" $selected>$text</a></li>
</ul>
';
}?>

</div>All the signs and combination with html is overwelming me

Be careful about what you're putting in the loop here. For each item in the array above, you're going to get one loop iteration, meaning you'll repeat all of the HTML inside the loop.

Right now, you're repeating "<ul><li></li></ul>" for each menu item. I'm assuming you probably want one list tag with several item tags...in that case, your code should basically look like this:


<div>
<ul>

<?php php code here ?>

</ul>
</div>
And within the PHP code, you would only echo the "<li>...</li>" part.

roberto-bbh
02-14-2010, 12:35 PM
So something like this

<div id="header-nav">
<ul>

<?php
$links = array(
'index.php' => 'Index',
'hosting-plans.php' => 'Hosting Plans'

);

foreach ($links as $url => $text)
{
$selected = ($_SERVER['PHP_SELF'] == $url ? 'class="current"' : '');
echo'



<li><a href="$url" $selected>$text</a></li>

';
}?>


</ul>
</div>

It works partially, it doesn't display the text from the variable $text and the url from $url because that line is echoed. How do I "un-echo" this variables so it displays the values ?

<li><a href="$url" $selected>$text</a></li>

My page is is with .php extension and the only part containing php is the above one, I don't have <? in the first line.

I tried

<li><a href="<? $url ?>" $selected>$text</a></li>
but that doesn't work !

Thank You very much for helping me out with this basic problem ( advanced for me :eek: )

Neseema M M
02-15-2010, 12:59 AM
Hi

Use double quote for echo and escape the quotes within the statement.Using single quote variable do not expand.

Use the code below:


<div id="header-nav">
<ul>

<?php
$links = array(
'index.php' => 'Index',
'hosting-plans.php' => 'Hosting Plans'

);

foreach ($links as $url => $text)
{
$selected = ($_SERVER['PHP_SELF'] == $url) ? 'class="current"' : '';
echo "



<li><a href=\"$url\" $selected>$text</a></li>

";
}?>


</ul>
</div>


Hope it helps...

mattle
02-15-2010, 10:26 AM
Use double quote for echo and escape the quotes within the statement.Using single quote variable do not expand.

Exactly. Or, like in my first example, use the nowdoc syntax to get both variable expansion and avoid having to escape all of your quotation marks within the string. Not to mention, it is generally considered a sloppy practice to keep quotes open across line breaks...

roberto-bbh
02-15-2010, 04:44 PM
Hmm, it works now, the link is generated and the title also, but it doesn't apply the 'class="current" ' to the button !

Could it be a problem regarding placement and execution order ?



<div id="header-nav">
<ul>

<?php
$links = array(
'index.php' => 'Home',
'hosting-plans.php' => 'Hosting Plans',
'domains.php' => 'Domains',
'web-design.php' => 'Web Design',
'about.php' => 'About Us',
'contact.php' => 'Contact',
'http://customers.url.com' => 'Client Area'
);

foreach ($links as $url => $text)
{
$selected = ($_SERVER['PHP_SELF'] == $url) ? 'class="current"' : '';
echo "



<li><a href=\"$url\" $selected>$text</a></li>

";
}?>


</ul>
</div>

mattle
02-15-2010, 06:10 PM
Hmm, it works now, the link is generated and the title also, but it doesn't apply the 'class="current" ' to the button !

Could it be a problem regarding placement and execution order ?

I'm thinking that the most likely culprit is that PHP_SELF isn't reporting exactly what we expect. Let's add some debugging (my change in green):


<div id="header-nav">
<ul>

<?php
$links = array(
'index.php' => 'Home',
'hosting-plans.php' => 'Hosting Plans',
'domains.php' => 'Domains',
'web-design.php' => 'Web Design',
'about.php' => 'About Us',
'contact.php' => 'Contact',
'http://customers.url.com' => 'Client Area'
);

foreach ($links as $url => $text)
{
$selected = ($_SERVER['PHP_SELF'] == $url) ? 'class="current"' : '';
echo "


<!-- {$_SERVER['PHP_SELF']} -->
<li><a href=\"$url\" $selected>$text</a></li>

";
}?>


</ul>
</div>


Add that line to the output and then post the generated source for the <ol> block...

roberto-bbh
02-15-2010, 06:16 PM
Ok

It adds <!-- /index.php --> for Home, <!-- /hosting-plans.php --> on Hosting Plans page, and so on, above each <li></li> in the list, for every page so I guess it works right ?

mattle
02-15-2010, 06:59 PM
Ok

It adds <!-- /index.php --> for Home, <!-- /hosting-plans.php --> on Hosting Plans page, and so on, above each <li></li> in the list, for every page so I guess it works right ?

Not quite. We're looking for string equality between "/index.php" == "index.php", and so on. These strings are not equal, so we must force them to match. Since PHP_SELF seems to have a forward slash at the beginning (which I had forgotten), there are two possible solutions. One, you can change your array so that all links look like this:


"/index.php" => "Home",


That will have the effect of changing your links from <a href="index.php"> to <a href="/index.php">.

Another possible solution would be to leave the array alone and simply modify our comparison to be the equivalent of "if PHP_SELF equals slash, followed by index.php". In code,


if ($_SERVER['PHP_SELF'] == "/$url")


Either one of those should do the trick.

roberto-bbh
02-15-2010, 07:12 PM
Yaweee, it works, I used the second method with if ($_SERVER['PHP_SELF'] == "/$url") and it works perfectly.

You're the man !

Thank You very much,
Roberto