dazbot Posted September 7, 2007 Share Posted September 7, 2007 Hi everyone, pleased to be here. I am a complete newby to php and Im trying to learn how to do something, here is what im trying to achieve. something like: if url="page1.php" then print("<img src="image1.gif" alt="image1">"); if url="page2.php" then print("<img src="image2.gif" alt="image2">"); if url="page3.php" then print("<img src="image3.gif" alt="image3">"); I know its wrong but you get the idea. Ive searched around google etc for some kind of example but I can't find anything, can anyone help me go in the right direction? Cheers Dazbot Quote Link to comment Share on other sites More sharing options...
xyn Posted September 7, 2007 Share Posted September 7, 2007 $url = explode("/", $_SERVER["SCRIPT_NAME"]); if($url == "page1.php") { echo("<img src=image1.jpg>"); } elseif($url == "page2.php") { echo("<img src=image2.jpg>"); } Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 7, 2007 Share Posted September 7, 2007 <?php switch($url) { case 'page1.php': echo '<img src="image1.gif" alt="image1">'; break; case 'page2.php': echo '<img src="image1.gif" alt="image1">'; break; case 'page3.php': echo '<img src="image1.gif" alt="image1">'; break; default: echo '<img src="imagedefault.gif" alt="image1">'; break; } ?> Quote Link to comment Share on other sites More sharing options...
dazbot Posted September 7, 2007 Author Share Posted September 7, 2007 <?php switch($url) { case 'page1.php': echo '<img src="image1.gif" alt="image1">'; break; case 'page2.php': echo '<img src="image1.gif" alt="image1">'; break; case 'page3.php': echo '<img src="image1.gif" alt="image1">'; break; default: echo '<img src="imagedefault.gif" alt="image1">'; break; } ?> thank you both for such a quick response, heres what I have so far with your suggestion ToonMariner <?php switch($url) { case 'index.php?department=44': echo '<img src="imgs/inc/1000mile.gif" alt="1000 mile">'; break; case 'index.php?department=45': echo '<img src="imgs/inc/muller.gif" alt="Muller">'; break; case 'index.php?department=46': echo '<img src="imgs/inc/noene.gif" alt="Noene">'; break; default: echo '<img src="imgs/inc/default.gif" alt="blah">'; break; } ?> but it displays the default image all the time. I have tried using full urls and just pagename but having no luck. thanks in advance Quote Link to comment Share on other sites More sharing options...
dazbot Posted September 7, 2007 Author Share Posted September 7, 2007 Just playing around with with your post as well xyn which is: <?php $url = explode("/", $_SERVER["SCRIPT_NAME"]); if($url == "index.php?department=44") { echo("<img src="imgs/inc/1000mile.gif" alt="1000 mile">"); } elseif($url == "index.php?department=45") { echo("<img src="imgs/inc/muller.gif" alt="Muller">"); } ?> but that gives me the error: Parse error: parse error in /web/inc/department.inc.php on line 121 thanks in advance Quote Link to comment Share on other sites More sharing options...
bronzemonkey Posted September 7, 2007 Share Posted September 7, 2007 On the pages you are trying to get to show these images, you will want the value of $url to be the same as the output that you get from this when it is included on your page (remove it when you've got the output): <?php var_dump($url); ?> And in xyn's post he didn't escape the " " within " ". ToonMariner used " " within ' ' so no escape is required. This is the correction to xyn's code: <?php $url = explode("/", $_SERVER["SCRIPT_NAME"]); if($url == "index.php?department=44") { echo("<img src=\"imgs/inc/1000mile.gif\" alt=\"1000 mile\">"); } elseif($url == "index.php?department=45") { echo("<img src=\"imgs/inc/muller.gif\" alt=\"Muller\">"); } ?> Hopefully that helps Quote Link to comment Share on other sites More sharing options...
xyn Posted September 7, 2007 Share Posted September 7, 2007 @ Bronzemonkey; Thanks for correcting the code heh, though i never test my coding. however i noticed a problem i forgot. using explode() creates an array of which we need to ammend like so: <?php $url = explode("/", $_SERVER["SCRIPT_NAME"]); if($url[1] == "index.php?department=44") { echo("<img src=\"imgs/inc/1000mile.gif\" alt=\"1000 mile\">"); } elseif($url[1]== "index.php?department=45") { echo("<img src=\"imgs/inc/muller.gif\" alt=\"Muller\">"); } ?> Quote Link to comment Share on other sites More sharing options...
dazbot Posted September 7, 2007 Author Share Posted September 7, 2007 you guys are great! thanks for all the responses but alas it still isn't working! When I tried : <?php var_dump($url); ?> it returns NULL ??????? so I searched around and found this: $uri = 'http' . (!empty($_SERVER['HTTPS']) ? 's' : null) . '://' . $_SERVER['HTTP_HOST'] . ($_SERVER['SERVER_PORT'] != 80 ? ":{$_SERVER['SERVER_PORT']}" : null) . $_SERVER['REQUEST_URI']; echo $uri; which gave me this address: http://www.domain.com/shop/index.php?department=44 however the altered code displays nothing (but doesn't give a parse error now!), I checked the page source for code but nothings there. <?php $url = explode("/", $_SERVER["SCRIPT_NAME"]); if($url[1] == "http://www.domain.com/shop/index.php?department=44") { echo("<img src=\"imgs/inc/1000mile.gif\" alt=\"1000 mile\">"); } elseif($url[1]== "http://www.domain.com/shop/index.php?department=45") { echo("<img src=\"imgs/inc/muller.gif\" alt=\"Muller\">"); } ?> Quote Link to comment Share on other sites More sharing options...
bronzemonkey Posted September 7, 2007 Share Posted September 7, 2007 Try this: <?php $url = $_SERVER['PHP_SELF']; if($url == "/index.php?department=44") { echo("<img src=\"imgs/inc/1000mile.gif\" alt=\"1000 mile\">"); } elseif($url == "/index.php?department=45") { echo("<img src=\"imgs/inc/muller.gif\" alt=\"Muller\">"); } ?> If it doesn't work, use var_dump again to see what value of url is required. If that still doesn't work, then we've already reached the limits of my PHP Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 8, 2007 Share Posted September 8, 2007 OMG what are you all doing???????? leading the poor lad astray switch is the order of the day when haveing MANY possible values - far better than lots of if statements!!! OK I was thinking your would not actually use the scriptname but a url var in my first post but as you made no mention I decided to leave to what you posted... Try this. <?php $dept = isset($_GET['department']) ? $_GET['department'] : NULL; switch($dept) { case 44: echo '<img src="imgs/inc/1000mile.gif" alt="1000 mile">'; break; case 45: echo '<img src="imgs/inc/muller.gif" alt="Muller">'; break; case 46: echo '<img src="imgs/inc/noene.gif" alt="Noene">'; break; default: echo '<img src="imgs/inc/default.gif" alt="blah">'; break; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.