Navigation Problem

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

i'm not sure if this is the right forum but,

many of the sites that i visit have a navigation part that shows them what page they are from i.e different from the rest, for example,

if you're on the home page

Home
Links
Contact

and if your on the links page

Home
Links
Contact

how is that done in php?
i'm sure there are a few ways that u can do it, what are some of them?

[edit]
and the easiest(?) way to do it?
[/edit]

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Basically, if you are including a common navigation file, you'll use simple IF statements.

home.php will declare a var $page='home';
links.php will declare a var $page='links';
etc...

all these pages will include nav.php

nav.php will look something like this:

<?php
if ($page='home') {
    echo \
"<b>Home</b>\n\";
}
else {
    echo \"<a href=\\"
home.php\\">Home</a>\n\";
}

if (
$page='links') {
    echo \"<b>Links</b>\n\";
}
else {
    echo \"<a href=\\"
links.php\\">Links</a>\n\";
}

// etc.
?>

Mark Hensler
If there is no answer on Google, then there is no question.

Busy's picture

He has: 6,151 posts

Joined: May 2001

another way is done with CSS, create a class (I call mine urhere) and add it into the link name of the page your on.

a.urhere:link {color:???; font-weight:bold; ...}
a.urhere:visited { same as above }
a.urhere:hover { color of normal link }
a.urhere:active { same as top or different color for click link}

and the link is:
the page
other page

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Isn't the comparison operator "=="? Or am I behind the times (again)? Confused

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

yeah ok, i tried that but, it don't work :'(

i did this :

<?php
       
if ($page='body.php') {
          echo
'<a href=\"index.php?body=body.php\" class=\"navlink_current\">Home</a>';
        } else {
          echo
'<a href=\"index.php?body=body.php\" class=\"navlink\">Home</a>';
        }
        if (
$body='services.php') {
          echo
'<a href=\"index.php?body=services.php\" class=\"navlink_current\">Services</a>';
        } else {
          echo
'<a href=\"index.php?body=services.php\" class=\"navlink\">Services</a>';
        }
        if (
$body='contact.php') {
          echo
'<a href=\"index.php?body=contact.php\" class=\"navlink_current\">Contact</a>';
        } else {
          echo
'<a href=\"index.php?body=contact.php\" class=\"navlink\">Contact</a>';
        }
       
?>

what's wrong?

Busy's picture

He has: 6,151 posts

Joined: May 2001

try
if ($page=='body')

no .php on page/file names

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

nah, it still don't work :'(

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Hmm, Renegade, that code looks awfully familiar... Wink

Busy's picture

He has: 6,151 posts

Joined: May 2001

that top $page should be $body shouldn't it

try this:

if ($body=='home') {
echo 'Home';
} else {
echo 'Home';
}

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

yay! thanx busy Smiling it works now Smiling

and yes abhi it looks similar, but it's not what u think it is lol Smiling

now i have another problem, i wanna add an  | at the end but it won't let me, how can i do it?

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

a "& nbsp ;"and"|" ^ the non breaking space did show up

Busy's picture

He has: 6,151 posts

Joined: May 2001

you want to write &nbsp; or just use one? (to display it in here you type &amp;nbsp;

I believe you just escape the extras like you do quotes "\|"

where do you want to add it?

**Where's Mark when you need him lol**

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

i wanna add it

<?php
if ($body=='body.php') {
  echo
'<a href=\"index.php?body=body.php\" class=\"navlink_current\">Home</a>'<strong>here</strong>;
} else {
  echo
'<a href=\"index.php?body=body.php\" class=\"navlink\">Home</a><strong>and here</strong>';
}
?>

so it looks likes this (the code that is):

<?php
echo '<a href=\"index.php?body=body.php\" class=\"navlink\">Home</a><strong>&amp;nbsp;</strong>';
?>

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

so it looks like

Home | Services | Contact

Busy's picture

He has: 6,151 posts

Joined: May 2001

I'm confused, which did you want to add there, a &nbsp; or | ?

in the above you have here ouside of the single quote

if you do this:
echo 'start ';
echo ' stop';
youd get spaces after the start and before the stop (note the gap between the end of the word and last quote)

if you want an operator, like *, +, = etc you can always stick it in a seperate echo, like so:

echo 'main stuff ';
echo '|';
...

[edit] ok you posted that second bit before i hit submit lol[/edit]

just add a space and | and another space before the end quote

if ($body=='body.php') {
echo 'Home | ';
} else {
echo 'Home | ';
}

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

i want to add an nbsp and a | like this nbsp;| so that i get
Home | Services | Contact

Busy's picture

He has: 6,151 posts

Joined: May 2001

see my last reply, I hit reply before seeing your second post

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

ahhh i got it Smiling thanx busy

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.