Guys, Need some help with this Image Code

Discussion in 'PHP' started by Andy_ameed, Nov 14, 2010.

  1. #1
    Here is the code..

    <img width="288" height="231" src="uploads/<?=$Threaddata['image'] ;?>" alt="<?=$Threaddata['username'] ;?>"/>
    Code (markup):
    here, this one shows the image if a user uploads an image, im trying to add a "php else function" feature where if the user doesnt upload an image a default image will be displayed.

    Please help out, Thanks in advance
     
    Andy_ameed, Nov 14, 2010 IP
  2. tnd8

    tnd8 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?php
    if ($Threaddata['image']) { ?>
       <img width="288" height="231" src="uploads/<?=$Threaddata['image'] ;?>" alt="<?=$Threaddata['username'] ;?>"/>
    <?php }
    else { ?>
       <b>something here</b>
    <?php }
    ?>
    
    PHP:
     
    tnd8, Nov 14, 2010 IP
  3. Andy_ameed

    Andy_ameed Active Member

    Messages:
    129
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    I have tried it, it results no error, but it not showing anything when i put the code. I tried to put an image between <b> ...</b> code.
     
    Andy_ameed, Nov 15, 2010 IP
  4. tnd8

    tnd8 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    <?php
    if ($Threaddata['image']) { ?>
       <img width="288" height="231" src="uploads/<?=$Threaddata['image'] ;?>" alt="<?=$Threaddata['username'] ;?>"/>
    <?php }
    else { ?>
       <img width="288" height="231" src="default_avatar.jpg" alt="<?=$Threaddata['username'] ;?>"/>
    <?php }
    ?>
    
    PHP:
     
    tnd8, Nov 15, 2010 IP
  5. Andy_ameed

    Andy_ameed Active Member

    Messages:
    129
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    Thanks a lot Tnd8. Its working now.. :)
     
    Andy_ameed, Nov 15, 2010 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    If your still having problems, try using, file_exists

    
    <?php $image_file = dirname(__FILE__) . '/uploads/' . $Threaddata['image'];
      if (file_exists($image_file)) // checks the upload directory for the image
        { ?>
       <img width="288" height="231" src="uploads/<?=$Threaddata['image'] ;?>" alt="<?=$Threaddata['username'] ;?>"/>
     <? } else { ?> // not found shows a default image 
    <img width="288" height="231" src="uploads/default.jpg" alt="default.jpg"/>
    <? } ?>
    
    PHP:
     
    MyVodaFone, Nov 15, 2010 IP
  7. Andy_ameed

    Andy_ameed Active Member

    Messages:
    129
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #7
    Thanks "MyVodaFone" both ways work perfectly. Thanks..
     
    Andy_ameed, Nov 15, 2010 IP