Navigation Question
Currently, for my navigation I am doing this:
<? if ($page == "links") { ?>
Links
<? } else { ?>
Links
<? } ?>
I have to do that for every single navigation link that I have, i'm sure there is another way of doing this, through loops or something.
Is there?
Busy posted this at 09:37 — 19th January 2003.
He has: 6,151 posts
Joined: May 2001
I do something similar, as every link is different there isn't any real shorter way (I could be wrong).
Instead of escaping php and doing html then php etc you could keep it all php and just echo (or print) the html line
<?php
if ($page == "links") {
echo "<span class=\"navlink_current\">Links</span>";
} else {
echo "<a href=\"index.php?page=links\" class=\"navlink\">Links</a>";
}
?>
zollet posted this at 12:37 — 19th January 2003.
He has: 1,016 posts
Joined: May 2002
You could do this with a loop.... Add the following code to the top of your PHP file...
<?php
//Place the menus in the order you want shown with their URL...
//No comma after the last menu listed
$menus = array(
\"Home\" => \"index.php?page=home\",
\"Services\" => \"index.php?page=services\",
\"Links\" => \"index.php?page=links\"
);
foreach($menus as $menu => $url) {
if(strtolower($_REQUEST[\"page\"]) == strtolower($menu)) {
$menu_lines .= \"<span class=\\"navlink_current\\">$menu</span>\";
} else {
$menu_lines .= \"<a href=\\"$url\\" class=\\"navlink\\">$menu</a>\";
}
}
?>
Then place <?= $menu_lines ?> where you'd like the menu.
Suzanne posted this at 16:33 — 19th January 2003.
She has: 5,507 posts
Joined: Feb 2000
*wink wink* try using lists instead of spans, too.
Renegade posted this at 22:05 — 19th January 2003.
He has: 3,022 posts
Joined: Oct 2002
I'm trying Zollets way cause it seems to be the easiest, but now I have two more questions.
1, It only prints "$menu";
if the url is index.php?page=home which is the very first page, how do you make it so that is prints "$menu"; when it's just index.php?
2, Is there anyway I can change the title in that loop function too?
Suzanne: I'll be getting onto using lists for my navitation in the near future I just need to can't figure them out yet
Renegade posted this at 22:11 — 19th January 2003.
He has: 3,022 posts
Joined: Oct 2002
I figured out the second question don't worry
Renegade posted this at 22:30 — 19th January 2003.
He has: 3,022 posts
Joined: Oct 2002
Why do spaces not work?
i.e. "Contact Me" => "index.php?page=contact"
I tried "Contact Me" => "index.php?page=contact" but that doesn't work either :S
zollet posted this at 00:08 — 20th January 2003.
He has: 1,016 posts
Joined: May 2002
I'm not sure I fully understand your problem, but I _think_ this might solve it. Add the PHP code below before to the top of the earlier posted PHP code...
<?php
if(!isset($_REQUEST[\"page\"])) {
$_REQUEST[\"page\"] = \"home\";
}
?>
Try this....
"Contact Me" => "index.php?page=contact%20me"
Renegade posted this at 04:08 — 20th January 2003.
He has: 3,022 posts
Joined: Oct 2002
sorry i was talking more of this part:
"Contact Me" => "index.php?page=contact"
The url is fine, sorry should have been more clear in my question
Mark Hensler posted this at 07:44 — 20th January 2003.
He has: 4,048 posts
Joined: Aug 2000
try this:
<?php
// keys for our sub-arrays
define(\"_TEXT\", 0);
define(\"_URL\", 1);
function Format_Link_Text($link)
{
return URLEncode(strtolower($link));
}
$menus = array(
array(\"Home\", \"index.php?page=home\"),
array(\"Services\", \"index.php?page=services\"),
array(\"Links\", \"index.php?page=links\")
);
foreach($menus as $link) {
if (Format_Link_Text($_REQUEST[\"page\"]) == Format_Link_Text($link[_TEXT])) {
$menu_lines .= \"<span class=\\"navlink_current\\">$menu</span>\";
} else {
$menu_lines .= \"<a href=\\"\" . $link[_URL] . \"\\" class=\\"navlink\\">\" . $link[_TEXT] . \"</a>\";
}
}
?>
Mark Hensler
If there is no answer on Google, then there is no question.
Renegade posted this at 08:02 — 20th January 2003.
He has: 3,022 posts
Joined: Oct 2002
Here's what I have so far:
<?php
//Place the menus in the order you want shown with their URL...
//No comma after the last menu listed
$menus = array(
\"Home\" => \"index.php?page=home\",
\"Links\" => \"index.php?page=links\",
\"Computer&nbsp;Section\" => \"index.php?page=comsect\",
\"Kung&namp;bsp;Fu Section\" => \"index.php?page=kfsect\",
\"Praying&nbsp;Mantis History\" => \"index.php?page=mantishistory\",
\"Your&nbsp;Rights\" => \"index.php?page=rights\",
\"Jokes\" => \"index.php?page=jokes\",
\"About&nbsp;Me\" => \"index.php?page=aboutme\",
\"Contact&nbsp;Me\" => \"index.php?page=contact\",
\"Disclaimer\" => \"index.php?page=disclaimer\"
);
if(!isset($_REQUEST[\"page\"]))
{
$_REQUEST[\"page\"] = \"home\";
}
foreach($menus as $menu => $url)
{
if(strtolower($_REQUEST[\"page\"]) == strtolower($menu))
{
$menu_lines .= \"<tr>\n<td>\n<span class=\\"navlink-current\\">$menu</span>\n</td>\n</tr>\";
$title = $menu;
$heading = $menu;
}
else
{
$menu_lines .= \"<tr>\n<td>\n<a href=\\"$url\\" class=\\"navlink\\">$menu</a>\n</td>\n</tr>\";
}
}
?>
Which seems to work perfectly with the exceptions of the ones with spaces I tried but that doesn't work either
Busy posted this at 23:02 — 20th January 2003.
He has: 6,151 posts
Joined: May 2001
did you try %20 instead of ?
you also have a typo on the kung fu one (&namp;bsp;)
Renegade posted this at 23:39 — 20th January 2003.
He has: 3,022 posts
Joined: Oct 2002
<?php
$root = \"index.php?page=\";
$menus = array (
\"body\" => \". : CSS Help : .\",
\"intro\" => \"Introduction\",
\"syntax\" => \"Syntax\",
\"howto\" => \"How To\",
\"background\" => \"Background\",
\"text\" => \"Text\",
\"font\" => \"Font\",
\"border\" => \"Border\",
\"margin\" => \"Margins\",
\"padding\" => \"Padding\",
\"list\" => \"Lists\"
);
if(!isset($_REQUEST[\"page\"]))
{
$_REQUEST[\"page\"] = \"home\";
}
foreach($menus as $url => $menu)
{
if(strtolower($_REQUEST[\"page\"]) == strtolower($url))
{
$menu_lines .= \"<span class=\\"navlink-current\\">$menu</span><br />\n\";
$page_title = \"$menu\";
}
else
{
$menu_lines .= \"<a href=\\"\" . $root . $url . \"\\" class=\\"navlink\\">$menu</a><br />\n\";
}
}
?>
The code has been modified a bit, but everything works jsut fine and dandy
Thanks to everyone who helped
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.