-3

I have a basic display php table for images however some of the columns in the table are empty.

$result = mysql_query("SELECT * FROM table2
    WHERE bid=$bid
    AND fhid=$fhid
    ") or die(mysql_error());

echo "<table align='center'>";
while($row = mysql_fetch_array( $result )) { 
    echo "<tr><td>";
    echo '<a href="'.$row['photo1'].'"target="_blank"><img src="'.$row['thumb1'].'" width="180" height="250" alt="'.$row['name'].' '.$row['alttext'].'"/></a>';
    echo "</td><td>";
    echo '<a href="'.$row['photo16'].'"target="_blank"><img src="'.$row['thumb16'].'"width="180" height="250"alt="'.$row['name'].' '.$row['alttext'].'"/></a>';
    echo "</td><td>";
    echo '<a href="'.$row['photo2'].'"target="_blank"><img src="'.$row['thumb2'].'" width="180" height="250"alt="'.$row['name'].' '.$row['alttext'].'"/></a>';
    echo "</td></tr>";
} 
echo "</table>";

If for example the row with photo16 was empty in the db table how can I change this script to skip the image without showing a missing image on the page and carry on to photo2.

I have checked all around and had no luck with NULL as it stops displaying the whole row. Thanks in advance.

EhsanT
  • 2,077
  • 3
  • 27
  • 31
  • 5
    First **stop** using deprected `mysql_*` API. use `mysqli_*` or `PDO` with prepared Statements. Second you have to ceck if the file exist and then decide if you create the link or not – Jens Mar 28 '17 at 13:54
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Mar 28 '17 at 14:09

3 Answers3

0

Try to add condition inside the while cycle if row is not empty. Something like this:

if(!empty($row)){
 echo "<table align='center'>";
 echo "<tr><td>";
 echo '<a href="'.$row['photo1'].'"target="_blank"><img src="'.$row['thumb1'].'" width="180" height="250" alt="'.$row['name'].' '.$row['alttext'].'"/></a>';
 echo "</td><td>";
 echo '<a href="'.$row['photo16'].'"target="_blank"><img src="'.$row['thumb16'].'"width="180" height="250"alt="'.$row['name'].' '.$row['alttext'].'"/></a>';
 echo "</td><td>";
 echo '<a href="'.$row['photo2'].'"target="_blank"><img src="'.$row['thumb2'].'" width="180" height="250"alt="'.$row['name'].' '.$row['alttext'].'"/></a>';
 echo "</td></tr>";
 }
janfitz
  • 1,183
  • 12
  • 21
0

Just check if isset the var you need to controll ($row['photo16'])

    if (isset($row['photo16']) && strlen($row['photo16']) > 0) {
       echo '<a href="'.$row['photo16'].'"target="_blank">
               <img src="'.$row['thumb16'].'"width="180" height="250"alt="'.
                     $row['name'].' '.$row['alttext'].'"/></a>';
    }
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • @JayBlanchard . your suggestion is correct but (if i understand correcly) seems that the question is related to a single image and not to all the row ... could be i'm wrong . – ScaisEdge Mar 28 '17 at 14:11
  • I thought about it more and it seems the OP is talking about skipping a column, not a row. You'd have to loop through the columns *inside* the `while` loop to achieve what the OP is after. – Jay Blanchard Mar 28 '17 at 14:11
  • He is speaking about skip photo16 but show photo2 .. is for this .. i have proposed this answer .. should be better ask to the OP .. .. – ScaisEdge Mar 28 '17 at 14:12
  • OP says what s/he wants in the title of the post. And here, *"If for example the row with photo16 was empty in the db table how can I change this script to skip the image without showing a missing image on the page and carry on"* photo16 is just an example. – Jay Blanchard Mar 28 '17 at 14:13
  • the OP in question body say "If for example the row with photo16 was empty in the db table how can I change this script to skip the image without showing a missing image on the page and carry on to photo2." – ScaisEdge Mar 28 '17 at 14:15
  • Note the words ***"for example"*** – Jay Blanchard Mar 28 '17 at 14:15
  • You can provide youe answer and let to the user the choice .. – ScaisEdge Mar 28 '17 at 14:16
  • I was just trying to help you out. – Jay Blanchard Mar 28 '17 at 14:17
  • I thanks you very much ...but the answer i provided is based on what i have understand .. could be i fail – ScaisEdge Mar 28 '17 at 14:18
  • Thankyou "scaisEdge" have just tried it and it works perfect. Thanks again. – user1729365 Mar 28 '17 at 14:38
0

check with !empty() function

like below. If not empty then it will print

if (!empty($row['photo16'])  > 0) {
       echo '<a href="'.$row['photo16'].'"target="_blank">
               <img src="'.$row['thumb16'].'"width="180" height="250"alt="'.
                     $row['name'].' '.$row['alttext'].'"/></a>';
    }
krishn Patel
  • 2,579
  • 1
  • 19
  • 30