Forum Home
Press F1
 
Thread ID: 87355 2008-02-18 05:38:00 Help with coding PHP, variables & $_GET depending on the page URL Chilling_Silence (9) Press F1
Post ID Timestamp Content User
641394 2008-02-18 05:38:00 Hi all,

So, heres the deal.

Im writing a website but thought I'd have a bit of a play around with PHP more this time.

I understand Im likely going to have to play around with some IF, ELSEIF or ELSE statements to get what I want but yeah..

In a nutshell:
You go to websiteX
The navigation looks like this:

<ul class="navmenu">
<li><a class="current" href="index.php">Home</a></li>
<li><a href="contact.php">Contact Us</a></li>
<li><a href="about.php">About Us</a></li>
</ul>

So here's the first example where Im going to want to (Depending on the URL typed) have:

<a class="current" href="contact.php">Contact Us</a>
instead of it being the Index link which has class=current set. This just highlights the link in the nav menu.

Next, what Im hoping to do is have the header part of the page, so if the URL were:
www.website.com/index.php?p=contact
It will show a different Image in the header part of the website. Is this likely to be terribly difficult?
While playing around / learning about $_GET variables I began to write something like this:

<p>Welcome <?php echo $_GET["p"]; ?> </p>
<p>
<?php
if($_GET['p'] == 1){
$message = 'I dont know who you are';
}
else{
$message = 'Nice to see you today';
}
; ?>
<?php echo $message; ?>
How would I put an image in there instead of a message? I can name the pictures so if they goto URL:
website.com/index.php?p=contact
Then the picture will also be called "contact.jpg" that is to be displayed in the header.
Currently there's looking to be about 10 pages.

Lastly, Im looking to get the main 'body content' (minus the header / footer / nav and a few other little things) by using the include function & the URL variable, just not entirely sure how to go about this...
Something like:
<?php
include '$_GET["p"].php';
?>

Have been doing quite a bit of reading on w3schools.com and a couple of other websites too, nothing covers *quite* what Im looking for thou :-/

Any assistance would be greatly appreciated

Kind regards


Chill.
Chilling_Silence (9)
641395 2008-02-18 06:16:00 Give me a shout on msn later this evening, I'll help you through it.

Edit: And note that your 'footer' bit is very, very poor security practice. You should be sanitising all user input, no exceptions.
Erayd (23)
641396 2008-02-18 10:17:00 <?php

$images = array(
'index' => '/images/index_img.png',
'content' => '/images/content_img.png'
);

if (array_key_exists('p', $_GET) and array_key_exists($_GET['p'], $images)) {
$my_image = $images[$_GET['p']];
} else {
$my_image = '/images/default_img.png';
}

?>

<img src="<?php echo $my_image; ?>" />


You may also want to consider using $_SERVER['PATH_INFO'] instead of specifying the page in a GET variable. This would let you use the URL:

www.site.com/index.php/content

and $_SERVER['PATH_INFO'] would then contain '/content', the extra path after the file. This is much more in keeping with the way browsers and web crawlers expect content to be set out. Be aware that relative links in pages using this method should be avoided - the browser treats index.php like a normal directory since it seems to be one.
TGoddard (7263)
1