Fetching mysql records to a html table using PHP

耗尽温柔 提交于 2019-12-08 10:40:12

问题


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>

回答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>");
}



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7836291/fetching-mysql-records-to-a-html-table-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!