如何限制 PHP While 循环的输出?

发布于 2024-11-28 03:06:52 字数 720 浏览 2 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(3

转身泪倾城 2024-12-05 03:06:52

这将在每 5 条记录后添加更多 br's 和 1 hr:

$cnt = 0;
while($row=mysql_fetch_array($sql))
{

  $id=$row['id'];
  $column=$row['column'];

  echo $id . '<br>';
  echo $column . '<br>';

  if($cnt % 5 == 0) echo "<br><br><hr><br><br>";

  $cnt++;
}   

This will add more br's and a hr after each 5 records:

$cnt = 0;
while($row=mysql_fetch_array($sql))
{

  $id=$row['id'];
  $column=$row['column'];

  echo $id . '<br>';
  echo $column . '<br>';

  if($cnt % 5 == 0) echo "<br><br><hr><br><br>";

  $cnt++;
}   
二智少女猫性小仙女 2024-12-05 03:06:52

这是您可以采取的一种(不是很复杂)方法:

$group_size = 5;
$tracking_variable = 0;
while ($row = function_to_fetch_row()) {
    if ($tracking_variable == 0) {
        // logic for the start of a grouping
    }
    // logic to display the row
    $tracking_variable = ($tracking_variable + 1) % $group_size;
    if ($tracking_variable == 0) {
        // logic for the end of a grouping
    }
}
if ($tracking_variable > 0) {
    // logic for the end of the final grouping
}

Here is one (not very sophisticated) approach you could take:

$group_size = 5;
$tracking_variable = 0;
while ($row = function_to_fetch_row()) {
    if ($tracking_variable == 0) {
        // logic for the start of a grouping
    }
    // logic to display the row
    $tracking_variable = ($tracking_variable + 1) % $group_size;
    if ($tracking_variable == 0) {
        // logic for the end of a grouping
    }
}
if ($tracking_variable > 0) {
    // logic for the end of the final grouping
}
苏别ゝ 2024-12-05 03:06:52

您希望将此数据保存在变量中,并在循环外部引用它。

试试这个:

$data = array();
while($row=mysql_fetch_array($sql))
{
    $data[$row['id']] = $row['column'];
}

现在你可以只显示特定的行:

print $data[$someRow];

You want to save this data in a variable, and reference it outside of the loop.

Try this:

$data = array();
while($row=mysql_fetch_array($sql))
{
    $data[$row['id']] = $row['column'];
}

And now you can just display specific rows:

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