November 17, 200817 yr Hey everyone! I have a site that is put together with php includes. The header is inserted into the document with an include, the footer and and the copyright area are as well. The main body is the only area that is on the page. Now, part of header.inc.php is the text buttons. If I were doing this in a straight .html document. I would change the styling of the text button when the visitor is on that page so show them where they are. But with a php include, the button bar is generic. How can I have it so that the PHP include figures out the visitor is on index.php and hilights the Home button and so on and so forth? Is this easy to do? Please advise. Thanks! -Seth
November 17, 200817 yr Hello You can work out which page your on using: $pageName = basename($_SERVER['SCRIPT_NAME']); If your using <ul><li>'s you can try : <ul> <li><a href="index.php" <?php if($pageName == "index.php"){ echo 'id="pageselected"';} ?>>Welcome</a></li> ect...... </ul> Rob
November 17, 200817 yr Author I don't think I quite understand Rob? I'm not using lists. Just text links. Could you give me an example of this one? $pageName = basename($_SERVER['SCRIPT_NAME']); How would I write this out? What is SCRIPT_NAME? What script? is $pageName the url? like index.php? And if it is then how do I tell it what style it should be if it is? Does that make any sense?
November 17, 200817 yr Hello $_SERVER['SCRIPT_NAME'] holds the full path and filename of the current script eg /root/phpstuff/siteone/index.php. The values in the $_SERVER array are created by the server automatically. basename chops off the path + returns the page name - index.php so : $pageName = basename($_SERVER['SCRIPT_NAME']); - $pageName will now hold the name of the current page. You can use php if statements to show/hide html code eg <?php $pageName = basename($_SERVER['SCRIPT_NAME']); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Index</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <h1>Some heading or other</h1> <?php if($pageName == "index.php") { ?> <h2>This is index.php</h2> <?php } else { ?> <h2>This page is not index.php</h2> <?php } ?> </body> </html> You can use this with the id selector. If you have an id called highlight, which is used to highlight the current page : <p><a href="index.php" <?php if($pageName == "index.php"){echo 'id="highlight"';} ?> >Home </a></p> <p><a href="about.php" <?php if($pageName == "about.php"){echo 'id="highlight"';} ?> >About Us</a></p> Hope that helps Rob
November 18, 200817 yr Hello 'SCRIPT_NAME' is the array key that accesses the current scripts path. You just use 'SCRIPT_NAME' , its server created - so its already there. http://uk2.php.net/manual/en/reserved.variables.server.php has some other $_SERVER array keys. You can test what you get by - copy/paste the next bit and save as testing.php + test in brower. <?php echo "{$_SERVER['SCRIPT_NAME']}<br />"; $thisPage = basename($_SERVER['SCRIPT_NAME']); echo "{$thisPage}"; ?> Try replacing 'SCRIPT_NAME' with some of the other $_SERVER array key's + see what you get. Rob
Create an account or sign in to comment