Jump to content

display image depending on url


dazbot

Recommended Posts

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

Link to comment
Share on other sites

<?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;
}
?>

 

 

 

Link to comment
Share on other sites

<?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

 

 

 

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@ 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\">");
}

?>

Link to comment
Share on other sites

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\">");
}

?>

 

 

 

Link to comment
Share on other sites

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  :(

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.