if (thispage == true)<

liunx

Guest
Ok, this might be a little confusing but I'll try my best to explain it as clearly as I can.

I have an image that I want to change depending on which page the user is on.

Like so...


_____
[ ]
| |
|IMAGE|
| |
[_____]
texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext texttext



now say that the page that the user is browsing in the 'about' page (<!-- m --><a class="postlink" href="http://mysite.com/about.php">http://mysite.com/about.php</a><!-- m -->) of my site.
that image should change to the 'about' image


_____
[ ]
|ABOUT|
|IMAGE|
| |
[_____]
texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext texttext


but if the user is browsing the 'news' page (<!-- m --><a class="postlink" href="http://mysite.com/news.php">http://mysite.com/news.php</a><!-- m -->) of the site, the image should change to the news image


_____
[ ]
|NEWS |
|IMAGE|
| |
[_____]
texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext texttext



does this all make since? and if so, how can i do this using php?do something like this:
$picname = preg_split('!/!',$_SERVER['PHP_SELF'],-1,PREG_SPLIT_NO_EMPTY);
$picname = preg_replace('/php$/','/gif/',$picname[sizeof($picname)]);
$_SERVER['PHP_SELF'] returns "the filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address <!-- m --><a class="postlink" href="http://example.com/test.php/foo.bar">http://example.com/test.php/foo.bar</a><!-- m --> would be /test.php/foo.bar."
So you would get something like this: /dirname/news.php
Then split it on the /'s, and take the last one returned (news.php).
Then use preg_replace to replace the 'php' with 'gif' to make it news.gif and name your images accordingly.

EDIT: 2 things...#1: if ALL your pages are in the root dir (like in your 2 examples, you could simply use 2 preg_replace functions like this:
$picname = preg_replace('/php/','/gif/',preg_replace('!/!','//',$_SERVER['PHP_SELF']))
This will NOT work if it is in a dir.

#2:To USE the newly found picname, do this:<img src='http://www.htmlforums.com/archive/index.php/imges/<?php echo $picname; ?>' width='??' height='??' />if they are all different pages then just hard code it. so far I haven't seen any code that is used with all pages?

is there a header that you are trying to change? tha tall pages load?

if so then you could also do a simple switch case statement to show the image.

switch($_SERVER['SCRIPT_NAME']){
case "news.php":
image = "news.jpg";
break;
case "about.php":
image = "about.jpg";
break;
default:
image = "whatever.jpg";
break;
}
echo "<img src=http://www.htmlforums.com/archive/index.php/\"$image\">";

you should get the idea$_SERVER['SCRIPT_NAME'] returns '/dir/subdir/filename.ext' so you would need to set up your 'cases' accordingly.
This would be far easier than my way, especially if there aren't too many files. Also, make sure that in each 'case' you set $image = 'whatever', and not just image = 'whatever'oh yes, good catch Aaron, I forgot the dollar sign for the variable.

unfortunatly, all the variables will have the path or directory the file is in.Originally posted by scoutt
unfortunatly, all the variables will have the path or directory the file is in.
right, but that is why the code needs to look like this:case "/dir/subdir/news.php":
or if it's in the root dir:
case "/news.php":thanks guys, i don't have a chance to check it out right now. i'm trying to get a site layout together right now, but i'll test it soon and give you all an update on how it went.
 
Back
Top