SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast paramedic's Avatar
    Join Date
    Oct 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    displaying varying amounts of images

    Hi guys

    I am having a bit of a problem figuring out how to display random amounts of pictures

    I allow people to upload up to 5 photos to my website. I store the file name in a database and the actual photos seperately.

    At the moment i am displaying the photos using the following code.


    Print "<td class=border width=20% valign=top rowspan=2 ><img src=http://www.propertyadsdirect.com/images/".$info['pic'] ."></a> <br>";
    Print "<img src=http://www.propertyadsdirect.com/images/".$info['pic2'] ."></a> <br>";
    Print "<img src=http://www.propertyadsdirect.com/images/".$info['pic3'] ."></a> <br>";
    Print "<img src=http://www.propertyadsdirect.com/images/".$info['pic4'] ."></a> <br>";
    Print "<img src=http://www.propertyadsdirect.com/images/".$info['pic5'] ."></a> <br>";
    Print "</td >";

    However if the user has not uploaded the full 5 photos a image is displayed in its place to indicate that an image is supposed to be there. How can I check that a photo is present and if not stop the site displaying this other image.

    Many thanks for your help.

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use file_exists to check whether the named file exists.

  3. #3
    SitePoint Enthusiast paramedic's Avatar
    Join Date
    Oct 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply. I expected something more complicated than that. I will give it a go tomorrow and let you know how it went.

    Many thanks

  4. #4
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Complicating your life is one thing PHP doesn't try to do.

    A bit of advice: download the CHM (Windows Help) version of the PHP Manual and keep it open while coding. You can usually find a function in seconds that does whatever you want to do.

    Good luck!

  5. #5
    SitePoint Enthusiast paramedic's Avatar
    Join Date
    Oct 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I have tried adding file_exists and a loop ( not sure if it is right as it is the first time I have used a loop) and I am reciving the message

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in E:\domains\p\propertyadsdirect.com\user\htdocs\showall.php on line 110

    Line 10
    refers to Print "<td class=border width=20% valign=top rowspan=2 ><img src=http://www.propertyadsdirect.com/images/".info.$piccy"></a> <br>";


    ******************
    CODE
    $piccy = array("pic","pic2","pic3","pic4","pic5");

    if (file_exists($piccy))
    {
    Print "<td class=border width=20% valign=top rowspan=2 ><img src=http://www.propertyadsdirect.com/images/".info.$piccy"></a> <br>";
    }
    else
    {

    any ideas what I am doing wrong?


    Many thanks

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Hi
    file_exists looks for the file on the server so:

    PHP Code:
    $pic1 "http://www.propertyadsdirect.com/images/"$info['pic2']; 

    if(
    file_exists($pic1)) {
        echo  
    '<img src="'$pic1 .'" />';

    Something like that should work.....

    *disclaimer - I'm goosed so that might be right!*
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $piccies = array('pic''pic2''pic3''pic4''pic5');
    $base 'http://www.propertyadsdirect.com/images';
    echo 
    '<td>';
    foreach(
    $piccies as $piccy){
        if (
    file_exists($piccy)){
            echo 
    "<img src=\"{$base}/{$piccy}\" /><br />";
        }
    }
    echo 
    '</td>'
    *disclaimer: It's right
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by arkinstall View Post
    *disclaimer: It's right
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    SitePoint Enthusiast paramedic's Avatar
    Join Date
    Oct 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for your help. I have changed my code to the code you suggested, but unfortunately no pictures are displayed.
    I used the following code to get the info from the database

    $data = mysql_query("SELECT * FROM propertydetails WHERE id = '$houseId'")

    or die(mysql_error());


    while($info = mysql_fetch_array( $data ))

    Should I use $info in the code you gave me?


    Many thanks
    Last edited by paramedic; Oct 1, 2008 at 22:28.


Bookmarks

Posting Permissions

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