Navigation Question

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

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's picture

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>";
}
?>

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's picture

She has: 5,507 posts

Joined: Feb 2000

*wink wink* try using lists instead of spans, too. Wink

Renegade's picture

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 Laughing out loud

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

I figured out the second question don't worry Smiling

Renegade's picture

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

He has: 1,016 posts

Joined: May 2002

Quote: 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?

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\";
}
?>

Quote: 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

Try this....

"Contact Me" => "index.php?page=contact%20me"

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Quote: Originally posted by zollet

Try this....
[B]"Contact Me" => "index.php?page=contact%20me"

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's picture

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's picture

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&amp;nbsp;Section\" => \"index.php?page=comsect\",
            \"Kung&namp;bsp;Fu&nbsp;Section\" => \"index.php?page=kfsect\",
            \"Praying&amp;nbsp;Mantis&nbsp;History\" => \"index.php?page=mantishistory\",
            \"Your&amp;nbsp;Rights\" => \"index.php?page=rights\",
            \"Jokes\" => \"index.php?page=jokes\",
            \"About&amp;nbsp;Me\" => \"index.php?page=aboutme\",
            \"Contact&amp;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 &nbsp; but that doesn't work either Sad

Busy's picture

He has: 6,151 posts

Joined: May 2001

did you try %20 instead of &nbsp; ?

you also have a typo on the kung fu one (&namp;bsp;)

Renegade's picture

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 Laughing out loud

Thanks to everyone who helped Laughing out loud

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.