如何限制 PHP While 循环的输出?
我有一个 while 循环正在运行,它从 MYSQL 表返回值。它从查询中返回大约 90 个条目。我希望能够将这 90 个值分解并以 5 个为一组显示它们。这可能听起来令人困惑,所以让我分享一些代码示例来帮助澄清:
$con = mysql_connect("host", "username", "password") or die ("Unable to connect to server");
mysql_select_db("database") or die ("Unable to select database");
$sql = mysql_query("SELECT * FROM `table` WHERE column IS NOT NULL AND column <> ''");
这里我从表中提取出我需要的值。 此时,
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$column=$row['column'];
echo $id . '<br>';
echo $column . '<br>';
}
我已经运行了循环并显示了所有 90 个条目。如何提取这些值并以较小的块显示它们?如果我在 while 循环之外使用循环值,它只会给出集合中的最后一个值。是否有一种简单的方法可以使用列中唯一的 $id
值来获取循环内的特定分组?
I have a while loop running that is returning values from a MYSQL table. It returns about 90 entries from the query. My hope is to be able to break these 90 values up and display them in groupings of 5. This may sound confusing so let me share some code samples to help clarify:
$con = mysql_connect("host", "username", "password") or die ("Unable to connect to server");
mysql_select_db("database") or die ("Unable to select database");
$sql = mysql_query("SELECT * FROM `table` WHERE column IS NOT NULL AND column <> ''");
Here I am pulling the values out that I need from the table...
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$column=$row['column'];
echo $id . '<br>';
echo $column . '<br>';
}
At this point, I have run the loop and get the display of all 90 entries. How can I pull the values out and display them in smaller chunks? If I use the looped values outside of the while loop, it just gives me the last value from the set. Is there a simple way to use the unique $id
value from the column to get a specific grouping within the loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将在每 5 条记录后添加更多 br's 和 1 hr:
This will add more br's and a hr after each 5 records:
这是您可以采取的一种(不是很复杂)方法:
Here is one (not very sophisticated) approach you could take:
您希望将此数据保存在变量中,并在循环外部引用它。
试试这个:
现在你可以只显示特定的行:
You want to save this data in a variable, and reference it outside of the loop.
Try this:
And now you can just display specific rows: