0

I am trying to fetch my mysql records into a table, each cell must have one image only.

The problem is that the photo duplicated 5 times according to the number I need to fill each row.

Here is my code:

<table width="%" border="1" bordercolor="#F7F7F7" align="center" cellspacing="10" cellpadding="5">

       <tr>

       <?
       for($i=0;$i<=5;$i++)
       { 

         echo "<td width=125 height=125><img width=125 height=125 src=images/".$info['photo'] ."></td>"; 
         } }?>
       </tr>
     </table>

How can I correct this code to make each photo fetched one time for each cell?

***** EDIT *****

I put the while inside the table, and the fetching is okay now, but it still fetch in the same row, I need to make something to stop fetching until I have 5 cells in the same row, then continue to fetch in a new row.

 <table width="%" border="1" bordercolor="#F7F7F7" align="center" cellspacing="10" cellpadding="5">

       <tr>


       <?

       while($info = mysql_fetch_array( $data  )) 
 {  
 ?>

        <td width=125 height=125 ><img width=125 height=125 src=images/<? echo ($info['photo']); ?>></td>


     <?   } ?>
       </tr>
2

3 Answers 3

1

You're not changing $info['photo'] inside the loop. That's why you're echoing the same photo five times.

Depending how your code looks like you can modify your code like this:

$result = mysql_query($your_query);

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    echo("<td width=\"125\" height=\"125\"><img width=\"125\" height=\"125\" src=\" images/". $row["photo"] ."></td>");
}
Sign up to request clarification or add additional context in comments.

Comments

1

This does not seem to be all the relevant code to answer this question. You are looping 6 times in your 'for' loop and are fetching the same picture '$info['photo']' all 6 times.

You want to go to the next MySQL query every time you loop. So have a

$info = mysql_fetch_row($result)

inside your loop.

Comments

1

Try add $info = mysql_fetch_row() before echo inside your for-loop. Also check $info != false to ensure that you have enough images (5 items) to fill cells.

2 Comments

i tried that but it did not work, all images still not available
Maybe, $info = mysql_fetch_row($data) will work? For your edit - declare $i variable, assign 0 and inside loop add code if (++$i >= 5) {echo "</tr><tr>"; $i = 0; }

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.