While 循环仅在 HTML 中显示 1 个结果

发布于 2024-11-16 11:41:45 字数 693 浏览 0 评论 0原文

我在这个网站上看到过这个问题,但我的问题有一点点差异,我还没有看到答案。我的代码看起来像这样

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宫墨修音 2024-11-23 11:41:45

您必须将 $bc_display 字符串与 .= 连接起来,

如下所示:

$bc_display = '';
$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>';
}

?>

You have to concat your $bc_display string with .=

Like this:

$bc_display = '';
$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>';
}

?>
美羊羊 2024-11-23 11:41:45

您要在每次迭代时覆盖 $bc_display (删除以前存储的结果/行),您必须执行以下操作:

$bc_display .= '<table width="90%" align="center" cellpadding="4">
                  <tr>
                    <td width="93%">' .$bc_date. '<br />' .$bc. '</td>
                  </tr>
                </table>';

确保首先将 $bc_display 设置为“nothing”($ bc_display = ''; 在代码的第一行)。 *此外,您正在创建很多表,也许这是一个更好的选择(尽管不是您所要求的):

$bc_display = '<table width="90%" align="center" cellpadding="4">';
// your query;
while($row = mysql_fetch_array($sql_broadcast))
{
    // First 5 lines of your while loop;
    $bc_display .= '<tr><td width="93%">' .$bc_date. '<br />' .$bc. '</td></tr>';
}
$bc_display .= '</table>';

这将仅创建一个表,其中多行 < em>而不是多个表具有一行。这是更好的标记语言,但不是必需的。

You are overwriting $bc_display on every iteration (removing the previous stored result / row), you have to to do something like this:

$bc_display .= '<table width="90%" align="center" cellpadding="4">
                  <tr>
                    <td width="93%">' .$bc_date. '<br />' .$bc. '</td>
                  </tr>
                </table>';

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):

$bc_display = '<table width="90%" align="center" cellpadding="4">';
// your query;
while($row = mysql_fetch_array($sql_broadcast))
{
    // First 5 lines of your while loop;
    $bc_display .= '<tr><td width="93%">' .$bc_date. '<br />' .$bc. '</td></tr>';
}
$bc_display .= '</table>';

This will create only one table, with multiple rows instead of multiple tables having one row. This is better markup language, but not required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文