Quick PHP question
- Started
- Last post
- 25 Responses
- Dancer
I am no PHP developer but I have used includes and followed this tutorial to have a "selected" state on a nav item:
http://www.alistapart.com/articl…My one quick question is can you apply multiple $thispage titles to a page?
It does not work if I put:
< ? php $ thisPage="Ladbroke"; ? >
< ? php $ this Page="Recent Projects"; ? >(ignore spaces)
- Nairn0
I'm not really sure what you're asking - but the code you've written would declare $thisPage, set it to 'Ladbroke', then in the next line set it to 'Recent Projects', so if you then echo'd this, you'd get 'Recent Projects' and all mention of 'Ladbroke' would be lost.
What are you trying to do, exactly?
- emukid0
i think he is trying to have more than one navigation item selected.
- creez0
no,
<title><?php echo="the title"; ?></title>
- drgss0
echo = "the title"
lol
- JamesBoynton0
< ? php $ mainmenu="Ladbroke"; ? >
< ? php $ submenu="Recent Projects"; ? >
<html>
<head><?php if ($mainmenu=="Ladbroke")
echo " id=\"currentpage\""; ?><?php if ($submenu=="Recent Projects")
echo " id=\"currentpage\""; ?>- It would be a class you echo if its to be more than once on the pageJamesBoynton
- Dancer0
hmmmm
I think I know what you mean James.
- Dancer0
Great. Thanks James all working now
- No worries, im just learning mega basic php, its a bit of a bitch!JamesBoynton
- JamesBoynton0
Also to answer - "My one quick question is can you apply multiple $thispage titles to a page?"
I think you can, it would be messy and wouldnt make much sense but i think it would work.
< ? php $ thisPage="Ladbroke"; ? >
< ? php $ thisPage="Recent Projects"; ? >
<html>
<head><?php if ($thisPage=="Ladbroke")
echo " id=\"currentpage\""; ?><?php if ($thisPage=="Recent Projects")
echo " id=\"currentpage\""; ?>
- acescence0
you could use the same var if it were an array..
<?php
$thisPage = array ( "Ladbroke", "Recent Projects" );
if ( in_array ( "Ladbroke", $thisPage ) ) {
echo "found Ladbroke";
}?>
- armsbottomer0
i agree with acescence, if you are unsure about the structure of your nav, it might be a good idea to use an array to store your nav pages. this is especially useful if you're using subnavs.
also, regarding the article , you can use the $_SERVER['SCRIPT_NAME'] variable to test if a nav matches the current page's file name, if the name of the name is the same as the file name.
$title = basename($_SERVER['SCRIPT_NAME... '.php');
if your file was named ladbroke.php, this would output "ladbroke". when you match with your nav name, you can use lcfirst() on your nav name to make sure its lowercase.
you'll sometimes have to tweak the name of the nav your trying to match (like if your file was named "recentprojects.php" and you wanted to match with "Recent Projects") with basic regular expressions. so if your file is called recentprojects.php, you could do.
$nav = array("Ladbroke, "Recent Projects");
$title = basename($_SERVER['SCRIPT_NAME... '.php');
$page = lcfirst(str_replace(" ", "", $nav[1]);
if($page == $title){
echo " id=\"currentpage\"";
}
?>this method may seem like overkill, but if you're working with a bunch of pages and need to make a single change across multiple pages, its quite convenient.
- $_SERVER['SCRIPT_NAM... '.php'); should be $_SERVER['SCRIPT_NAM... '.php');armsbottomer
- Gah, i wish i understood what all this meant.JamesBoynton
- arr... $_SERVER['SCRIPT_NAM... '.php');armsbottomer
- it's actually not that difficult, regular expressions will always look like gibberish when you first approach them thougharmsbottomer
- oh yeah, i screwed up, strtolower() would work better than lcfirst()armsbottomer
- JamesBoynton0
armsbottomer, you sound like a person who knows php... which leads me to a question, i only know super super basic php..
I have a site with a menu in a navigation.php which is then included on all pages, is there any way that i can put the pages in different directories and it still to work. Damn, its hard to explain what i mean!
- armsbottomer0
i think i know what you mean. if you need to access that file from a child directory, you'd have to use ../ to go up a directory file.
for instance: if nav.php is in the root folder, and you need to access it from within a child folder, you'd do
include_once("../nav.php");
is that what you were referring to?
- JamesBoynton0
Yeah, thats what i mean, the problem is that in the navigation.php all the files link like so
<a href="../about.php>
<a href="../news.php>
<a href="../contact.php>But when im on the homepage for example those links try to go up a level rather than to the directory /about/about.php
I guess what im asking is is there way that the php knows what directory you are in (wether its one, two three etc levels up) and changes the link accordingly?
- acescence0
include_once ( $_SERVER [ ' DOCUMENT_ROOT ' ] . " nav.php ");
and inside nav.php, make all of your links relative to http root:
/ path / to / page.php
- JamesBoynton0
ahhh, cheers acescence... im not really sure what that all means, does it need to be an include_once or can i just be an include? im going to go try it out
cheers guys
- acescence0
it can just be an include. include_once just prevents php from including and parsing a file again that may have already been included elsewhere in a script. in this case you probably won't encounter that issue.
- surface0
agree with acescence.. you can also try setting a $root variable in your docType or db.php (whatever file is included throughout ALL your pages) and set the variable as the server root's URL.
it's a similar move that acescence went with but the single variable can be used for everything.. ex:
<img src="<?php echo $root; ?>/images/image.gif'>
<a href="<?php echo $root; ?>/../..">
- JamesBoynton0
Hey Acescence, just playing with that just now, i have added this to my index page
<?php include ( $_SERVER [ ' DOCUMENT_ROOT ' ] . " nav.php ");?>
and it doesnt appear to work as the nav doesnt show up on the page, ill testing locally in mamp... any ideas or have i made a noob syntax error?
- try the $root variable method and set it to http://localhost:888… or whatever your site directory is set to..surface
- JamesBoynton0
Hey Surface, that works a treat! is it a bad thing that evertime i go to load a page the link is for example:
<a href="http://localhost/_dev/tester/index.php">home</a>
I know when i upload it all i have to do is change the root variable (so i have it in an include) but i wondered if it was bad that the link was absolute rather than relative?
- surface0
the only problem that i can think of at the moment is if you wanted to hide certain directories, etc.. but that is where .htaccess Rewrite Rule comes around. are you familiar?