npsari Posted April 8, 2007 Share Posted April 8, 2007 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 More sharing options...
Zaid Posted April 8, 2007 Share Posted April 8, 2007 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) Link to comment https://forums.phpfreaks.com/topic/46178-a-problem-with-a-little-else-function/#findComment-224471 Share on other sites More sharing options...
npsari Posted April 8, 2007 Author Share Posted April 8, 2007 Ohh, that is a great info So, if I entered "" where there is no Image what should I change the code to... if ($Image==none) { Link to comment https://forums.phpfreaks.com/topic/46178-a-problem-with-a-little-else-function/#findComment-224483 Share on other sites More sharing options...
PC Nerd Posted April 8, 2007 Share Posted April 8, 2007 or empty($image) Link to comment https://forums.phpfreaks.com/topic/46178-a-problem-with-a-little-else-function/#findComment-224492 Share on other sites More sharing options...
npsari Posted April 8, 2007 Author Share Posted April 8, 2007 or empty($image) Can you show me please where to put this one in my code I dont know where to put this part Link to comment https://forums.phpfreaks.com/topic/46178-a-problem-with-a-little-else-function/#findComment-224493 Share on other sites More sharing options...
dhimok Posted April 8, 2007 Share Posted April 8, 2007 if (empty($Image) || $Image == "") { echo ""; } Link to comment https://forums.phpfreaks.com/topic/46178-a-problem-with-a-little-else-function/#findComment-224495 Share on other sites More sharing options...
npsari Posted April 8, 2007 Author Share Posted April 8, 2007 if (empty($Image) || $Image == "") { echo ""; } So, there is no need for the Else bit? Link to comment https://forums.phpfreaks.com/topic/46178-a-problem-with-a-little-else-function/#findComment-224498 Share on other sites More sharing options...
dhimok Posted April 9, 2007 Share Posted April 9, 2007 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 Link to comment https://forums.phpfreaks.com/topic/46178-a-problem-with-a-little-else-function/#findComment-224815 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.