0

I have a php script that is basically querying my database and providing details about each store, and including an image. The code pulls in the image fine, but currently if no image exists in the database, I have spacer image being inserted instead. Instead of using a spacer image though, is there a way for me to use a PHP 'if' statement inside of the php code to not render an image if the database has no image listed? Here is the basic query code:

<?php   
   if(strlen($query) >= $min_length){$query = htmlspecialchars($query); 
   $query = mysql_real_escape_string($query); 
   $raw_results = mysql_query("SELECT * FROM stores WHERE `TRAVEL` = '1' AND `STATE` = '$query'") or die(mysql_error());                                  
   if(mysql_num_rows($raw_results) > 0){
   while($results = mysql_fetch_array($raw_results)){

   echo "<table width='150' border='3'>
   <tbody><tr>
   <td>".$results['NAME']."<br>
   <img  align='left' src='images/images/".$results['IMAGE']."'> 
   </td>
   </tr></tbody>
   </table>";            
   }          
   }    else{ echo "No results were found";

   }           
   }    else{ echo " ".$min_length; }
?>

For the php if statement, I was thinking something along these lines:

<?php if ($results['IMAGE'] != '') {  ?>
<img src='images/icons/".$results['IMAGE']."' height="100" width="auto">
<?php }?>
3
  • 1
    Please stop using mysql functions - read here stackoverflow.com/q/12859942/5006692 Commented Mar 30, 2016 at 15:47
  • 1
    do not use htmlspecialchars on a mysql query. Commented Mar 30, 2016 at 16:04
  • Thanks, I'll remove and looking into modifying the mysql. Commented Mar 30, 2016 at 20:29

2 Answers 2

2

You're pretty close. Just use one more <?php ... ?> block in the img

<?php if ($results['IMAGE'] != '') {  ?>
  <img src='images/icons/<?php echo $results['IMAGE'] ?>' height="100" width="auto">
<?php }?>

You can use the alternative control flow syntax too. It reads a little nicer than spanning the { and } across multiple <?php tags

<?php if ($results['IMAGE'] != ''):  ?>
  <img src='images/icons/<?php echo $results['IMAGE'] ?>' height="100" width="auto">
<?php endif ?>

Shameless plug:

Generating HTML with PHP gets really ugly. At least in my opinion. To make the job a little nicer/easier, I made htmlgen, mirrored on packagist.

Sign up to request clarification or add additional context in comments.

2 Comments

heredoc syntax works "okay" as well for generating HTML if you don't want to include another library.
Thanks, but it does not seem to be working. Do I need to wrap the <?php if... ?> in quotes, or modify it since it is inside of the other php code?
1

Simply like this :

if(isset($results['IMAGE'])){

  echo "<table width='150' border='3'>
  <tbody><tr>
  <td>".$results['NAME']."<br>
  <img  align='left' src='images/images/".$results['IMAGE']."'>
 </td>";

}

1 Comment

Wouldn't this however not post the table if an image is non-existent, I actually want to keep the table even if the image is not in the table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.