Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    12
    Thanked 0 Times in 0 Posts

    deafult image if GET variable is not present

    Hey guys, I have a piece of code that enables me to show an image based on an id in the URL. My problem is, I want to show a default image if there is no ID given...Here is my code so far.

    Code:
    <a href="index.php?id=orange">link</a>
    PHP Code:
    <img src="http://www.website.co.uk/images/headers/<?php echo $_GET['id']; ?>_index.jpg" border="0">
    Hope I have explained myself properly

    Thanks in advance

  • #2
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    Lets assume the default image is called default_index.jpg, you could use this:

    PHP Code:
    <img src="http://www.website.co.uk/images/headers/<?php echo isset ($_GET['id']) ? $_GET['id'] : "default"?>_index.jpg" border="0">

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,352
    Thanks
    61
    Thanked 528 Times in 515 Posts
    Quote Originally Posted by iBall View Post
    Otherwise you could be displaying images that you would normally not want visible on your website.
    http://www.website.co.uk/images/headers/<?php echo isset ($_GET['id']) ? $_GET['id'] : "default"; ?>_index.jpg"

    With a semi hard coded file path I very much doubt it unless someone has FTP access.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,352
    Thanks
    61
    Thanked 528 Times in 515 Posts
    True, I do recommend the use of file_exists() as you say but I wouldn't worry about a remote person trying to display anything with a hard coded path is what I'm saying.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    mulder (05-29-2012)

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Sorry for the delay, I have implemented the code above, thank you for your time and help Appreciated!!!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •