Jump to content

A problem with a little else function


npsari

Recommended Posts

I have a field which asks the user to insert an image URL

 

This field is called Image

 

The initial value of the field is 'none'

 

So, if the users leaves it like that, it is saved in the databse as none

but if he puts an URL, it is not saved as none (obviously!)

 

So, I did this display code...

 

 

while($row = mysql_fetch_array($result))

{

if ($Image==none) {
     print "No Image\n";
} else {
     print ("<IMG src=\"$row[image]\" alt=\"Image\" border=\"0\" ><BR><BR>\n");
}

 

Is this code fine?

 

Because an Image keeps showing (even the missing shape one) :(

Link to comment
https://forums.phpfreaks.com/topic/46178-a-problem-with-a-little-else-function/
Share on other sites

1) there is a missing "}" at the end of your code

2)if that doesn't work, depending on your original code where you enter the data into the db, if you entered "" when there is no image then that's not a null value, there is a difference between "" and null. so try changing ($Image==NULL)

U can do it like this

 

while($row = mysql_fetch_array($result)) {

     if (empty($row[image]) || $row[image] == "") {
          print "No Image\n";
     } else {
          print "<img src=\"".$row[image]."\" alt=\"Image\" border=\"0\" >\n";
          //print '<img src="'.$row["Image"].'" alt="Image" border="0" />'; // <-- or like this
     }
} // end while

 

You can always use extract() or list() function to pull db fields into variables

 

while($row = mysql_fetch_array($result)) {
extract($row);

     if (empty($image) || $image == "") {
          print "No Image\n";
     } else {
          print "<img src=\"{$Image}\" alt=\"Image\" border=\"0\" >\n";
          //print '<img src="'.$Image.'" alt="Image" border="0" />'; // <-- or like this
     }
} // end while

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.