While 循环仅在 HTML 中显示 1 个结果
我在这个网站上看到过这个问题,但我的问题有一点点差异,我还没有看到答案。我的代码看起来像这样
$sql_broadcast = mysql_query("SELECT id, mem_id, bc, bc_date FROM bc WHERE
mem_id='$id' ORDER BY bc_date DESC LIMIT 50");
while($row = mysql_fetch_array($sql_broadcast)){
$mem_id = $row['mem_id'];
$bc = $row['bc'];
$bc_date = $row['bc_date'];
$bc_date = strftime("%b %d, %Y", strtotime($row['bc_date']));
$bc_display = '<table width="90%" align="center" cellpadding="4">
<tr>
<td width="93%">' .$bc_date. '<br />' .$bc. '</td>
</tr>
</table>';
}
?>
如果我只是回显或打印表格,循环就可以完美工作。但是,我希望能够在 html 页面内打印 $bc_display ,当我这样做时,只会检索到一个结果。我被难住了,我研究了建议的每一条线索,但我仍然被难住了。谢谢您的帮助!
I've seen this question asked on this site, but mine has a slight variance that I haven't seen answered. My code looks like this
$sql_broadcast = mysql_query("SELECT id, mem_id, bc, bc_date FROM bc WHERE
mem_id='$id' ORDER BY bc_date DESC LIMIT 50");
while($row = mysql_fetch_array($sql_broadcast)){
$mem_id = $row['mem_id'];
$bc = $row['bc'];
$bc_date = $row['bc_date'];
$bc_date = strftime("%b %d, %Y", strtotime($row['bc_date']));
$bc_display = '<table width="90%" align="center" cellpadding="4">
<tr>
<td width="93%">' .$bc_date. '<br />' .$bc. '</td>
</tr>
</table>';
}
?>
If I simply echo or print the table the loop works perfectly. However, I want to be able to print $bc_display inside the html page and when I do it that way only one result gets retrieved. I'm stumped, I've researched every thread that has been suggested and I'm still stumped. Thank you for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将 $bc_display 字符串与 .= 连接起来,
如下所示:
You have to concat your $bc_display string with .=
Like this:
您要在每次迭代时覆盖
$bc_display
(删除以前存储的结果/行),您必须执行以下操作:确保首先将 $bc_display 设置为“nothing”(
$ bc_display = '';
在代码的第一行)。 *此外,您正在创建很多表,也许这是一个更好的选择(尽管不是您所要求的):这将仅创建一个表,其中多行 < em>而不是多个表具有一行。这是更好的标记语言,但不是必需的。
You are overwriting
$bc_display
on every iteration (removing the previous stored result / row), you have to to do something like this:Make sure you set $bc_display to 'nothing' first (
$bc_display = '';
on the first line of your code). *Also, you are creating a lot of tables, maybe this is a better alternative (though not what you asked):This will create only one table, with multiple rows instead of multiple tables having one row. This is better markup language, but not required.