chris
chris

Reputation: 461

Displaying default image

I have a default image set in the db in the column default_image. If the default_image = 1 I want it to display the default image and if default_image = 0 I want text to say no image available. How do I make sure only the default image is displayed. Here is what I am doing now and it just displays any image that has the same link_id.

the column media_link is the address to the image.

<?php if (!empty($row['default_image'])){
echo "<a href='http://localhost/images".$row['media_link']."'><img src='http://localhost/images".$row['media_link']."'/></a>"; 
}
else{
    echo "<div align=\"center\">No image available</div>";
}?>

What do I do to make default_image "1" in the database display and nothing else? Thanks.

EDIT:
Here is another piece of code to show all the images for that link_id

if ($row['media_link']){
echo "<a href='http://localhost/t_images".$row['media_link']."'><img src='http://localhost/images".$row['media_link']."' /></a>"; 
}
else{
    echo "<div align=\"center\">No images available</div>";
}
}

there is 6 images for this current id
When i echo the vardump on this page the correct image has this
string(1) "1"
while the other 5 images "non default"
show this string(1) "0"
I am not sure how to show only the correct default image where it goes on the page in my first code. Does this info help at all?

EDIT AGAIN/FIXED:
I fixed it by adding ORDER BY default_image DESC to the end of my query. Don't know if that is the best thing to do but if anyone has any other suggestions i would be glad to hear them. Thanks everyone for all your help. You all gave great answers but in the end I just added the order by statement to my query and it works that way so I am happy. Thanks!

Upvotes: 0

Views: 124

Answers (3)

F&#233;lix Saparelli
F&#233;lix Saparelli

Reputation: 8719

In your situation, $row['default_image'] is a string. Of course, PHP is not a strongly typed language, but it still has to figure out what you mean. In your example, you are expecting this:

empty("0") === true

which does not happen. The empty string "" is empty, the number 0 is empty, but the string "0" is not. Instead you could use either of:

$row['default_image'] !== "0"
  // Uses strict comparison, which might not be what you expect
0+$row['default_image'] != 0
  // Uses casting, which might fail if the string doesn't contain a number
$row['default_image'] != "0" || !empty(0+$row['default_image'])
  // Uses short-circuiting, and should cover all (most) cases

There are other variants. Myself, I am not sure which I would choose between them: it depends on what you mean, exactly.

Upvotes: 2

Rukmi Patel
Rukmi Patel

Reputation: 2561

try this ...

<?php if (!empty($row['default_image']) && $row['default_image'] == 1){
         echo "<a href='http://localhost/images".$row['media_link']."'>
               <img src='http://localhost/images".$row['media_link']."'/></a>"; 
      }
      else{
         echo "<div align=\"center\">No image available</div>";
      }?>

Upvotes: 1

KoolKabin
KoolKabin

Reputation: 17653

Check Default_image against 1 instead of empty.
try: ..

<?php
if( isset($row['default_image']) && $row['default_image'] == 1){
echo "<a href='http://localhost/images".$row['media_link']."'><img src='http://localhost/images".$row['media_link']."'/></a>"; 
}else{
    echo "<div align=\"center\">No image available</div>";
}
?>

Upvotes: 1

Related Questions