使用 PHP 将 mysql 记录提取到 html 表
我正在尝试将我的 mysql 记录提取到表中,每个单元格必须只有一个图像。
问题是,根据我需要填充每行的数量,照片重复了 5 次。
这是我的代码:
<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>
如何更正此代码以使每个单元格的每张照片获取一次?
***** 编辑 *****
我把 while 放在表中,现在提取已经可以了,但它仍然在同一行中提取,我需要做一些事情来停止提取,直到表中有 5 个单元格为止同一行,然后继续获取新行。
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有在循环内更改
$info['photo']
。这就是为什么您要重复同一张照片五次。根据您的代码的外观,您可以像这样修改代码:
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:
这似乎不是回答这个问题的所有相关代码。您在 'for' 循环中循环了 6 次,并获取了同一张图片 '$info['photo']' 6 次。
每次循环时您都希望转到下一个 MySQL 查询。所以要有一个
内部循环。
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
inside your loop.
尝试在 for 循环内的 echo 之前添加 $info = mysql_fetch_row() 。另请检查 $info != false 以确保您有足够的图像(5 项)来填充单元格。
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.