Image depending on URL

Discussion in 'PHP' started by rosshj, Jan 14, 2006.

  1. #1
    Hello,
    I'm kind of new to PHP and I was wondering if anyone would know how to display a particular image depending on what section of the website you are in.

    example:

    (something.com/the-band) or (something.com/the-band/something) the photo would be "the-band.jpg" but if you were in (something.com/music) or (something.com/music/something) the photo would be "music.jpg"

    If anyone could shed some light, it would be much appreciated.

    Cheers,
    Ross
     
    rosshj, Jan 14, 2006 IP
  2. Riboflavin

    Riboflavin Well-Known Member

    Messages:
    1,091
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    155
    #2
    Would this be for anything, for example something.com/abc would search for abc.jpg even if it wasn't existant, or only for a few set things, IE your directories?
     
    Riboflavin, Jan 14, 2006 IP
  3. rosshj

    rosshj Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It would be something like:

    if
    url = the-band or the-band/something
    then <img src="someimage.jpg" />
    else if
    url = music or music/something
    then <img src="someotherimage.jpg" />

    I just don't know how to code it.
     
    rosshj, Jan 14, 2006 IP
  4. mitchandre

    mitchandre Peon

    Messages:
    313
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah that's simple to do in php. Probably, 3 lines of code or something.
     
    mitchandre, Jan 14, 2006 IP
  5. mitchandre

    mitchandre Peon

    Messages:
    313
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry I was wrong. It took 4 lines. :)

    <?php
    $graburl = $_SERVER['REQUEST_URI'];
    $searchstring = strpos($graburl,'x'); // x is what you want php to find in the url
    If ($searchstring === false) {echo "http://www.yourdomain.com/someimage.jpg";} // Type which image to display if php can't find it.
    else {echo "http://www.yourdomain.com/someimage.jpg";} // Type which image to display if php finds it.
    ?>
    PHP:
    You can pay me back in good reputation. But, my site likes links too. :rolleyes:

    P.S. You would place this where you would type the normal <img src="abovephpcode">
     
    mitchandre, Jan 14, 2006 IP
  6. rosshj

    rosshj Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Awesome, thanks a bunch. That worked with some tweaking. Here is my final code:

    $graburl = $_SERVER['REQUEST_URI'];
    
    if ($searchstring = strpos($graburl,'home')) {
    $photocolumn = "photocolumn_bg_home.jpg"; }
    
    else if ($searchstring = strpos($graburl,'the-band')) {
    $photocolumn = "photocolumn_bg_theband'.jpg"; }
    
    else if ($searchstring = strpos($graburl,'music')) {
    $photocolumn = "photocolumn_bg_music.jpg"; }
    
    else if ($searchstring = strpos($graburl,'blog')) {
    $photocolumn = "photocolumn_bg_blog.jpg"; }
    
    else if ($searchstring = strpos($graburl,'events')) {
    $photocolumn = "photocolumn_bg_ events.jpg"; }
    
    else if ($searchstring = strpos($graburl,'contact-us')) {
    $photocolumn = "photocolumn_bg_contactus.jpg"; }
    
    else if ($searchstring = strpos($graburl,'user')) {
    $photocolumn = "photocolumn_bg_user.jpg"; }
    
    else $photocolumn = "photocolumn_bg_home.jpg";
    Code (markup):
     
    rosshj, Jan 15, 2006 IP
  7. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #7
    U may find the switch function my work quicker
     
    Big 'G', Jan 15, 2006 IP
  8. rosshj

    rosshj Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    How does that work. I know naahting ;)
     
    rosshj, Jan 15, 2006 IP
  9. mitchandre

    mitchandre Peon

    Messages:
    313
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I doubt the switch command will speed anything up for you. You barely have that many cases. Once you get your code above 200 lines of else if statements than worry about it.
     
    mitchandre, Jan 15, 2006 IP