SRL Archive - Web Development

Some PHP help please


Gu3st...

2010-05-06 12:41:25

I have made a div named "menu" and a class inside menu that is named active
In the div="menu" part I have links to different sites and I want one of the links to be "active" (turn <a href="…"> into <a class="active">)
I have a file named menu.php which include all the links I need, so I just use the include procedure to include them in my code.

How do I make the code show the current link as active, while using menu.php?
I've though about using GET or POST commands, but is there a easier way to do it?

MylesMadness

2010-05-06 12:51:10

$_SERVER["REQUEST_URI"]; would help I would say

Frement

2010-05-06 12:51:35

From: Zyt3x
I have made a div named "menu" and a class inside menu that is named active
In the div="menu" part I have links to different sites and I want one of the links to be "active" (turn <a href="…"> into <a class="active">)
I have a file named menu.php which include all the links I need, so I just use the include procedure to include them in my code.

How do I make the code show the current link as active, while using menu.php?
I've though about using GET or POST commands, but is there a easier way to do it?


What? Please, what u want to do? :D And in english this time :) Explain what you want.

Gu3st...

2010-05-06 12:58:14

From: Frement
What? Please, what u want to do? :D And in english this time :) Explain what you want.
:(

I re-wrote the first post like 3 times before hitting submit new thread :p
lol.. I guess I just suck at explaining…

oh well..

menu.php:
[php] <ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Database</a></li>
</ul> [/php]

in index.php:
[php]<?php include("menu.php"); ?>[/php]

I want it to be like when I am at "Home" the link to Home would become "active" like this:
[php] <ul>
<li><a class="active">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Database</a></li>
</ul> [/php]

but when I'm at "About", I want the link to About to be active instead of Home:
[php] <ul>
<li><a href="#">Home</a></li>
<li><a class="active">About</a></li>
<li><a href="#">Database</a></li>
</ul> [/php]

Frement

2010-05-06 13:21:39

Menu.php
[PHP]<?php

function active($page) {
if (htmlspecialchars($_GET["page"]) == $page) {
return "active";
} else {
return "notactiveclass";
}
}

echo ' <ul>
<li><a href="#" class="' . active("home") . '>">Home</a></li>
<li><a href="#" class="' . active("about") . '>">About</a></li>
<li><a href="#" class="' . active("database") . '>">Database</a></li>
</ul>';

?>[/PHP]

And in ur index.php just include that to the nav.

This is copied from my site, i use this to get the page:
[PHP]<?php

if (isset($_GET["p"])) {
$p = htmlspecialchars($_GET["page"]);
if ( $p != "index" && $p != "menu" ) {
if (file_exists($p.".html")) {
$file = fopen($p.".html", "r");
echo fread($file, filesize($p.".html"));
fclose($file);
} elseif (file_exists($p.".php")) {
include($p.".php");
} else {
echo "404 Error. Page not found.";
}
} else {
include("home.php");
}
} else {
include("home.php");
}

?>[/PHP]

Gu3st...

2010-05-06 14:00:43

I see… Thanks :)
I will try to make it work, if it doesn't then I'll ask again :p

EDIT: Yay! It worked! :D I had to edit it a bit to make it work, but I couldn't have done it without you :) +rep

Frement

2010-05-06 15:01:07

From: Zyt3x
I see… Thanks :)
I will try to make it work, if it doesn't then I'll ask again :p

EDIT: Yay! It worked! :D I had to edit it a bit to make it work, but I couldn't have done it without you :) +rep


Dude no problem, im here to help! :)