PHP while 循环查找最后一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1247454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:43:16  来源:igfitidea点击:

PHP while loop find last row

php

提问by Tyler Carter

$sql = mysql_query("SELECT * FROM comments WHERE user = 1");

$i = 1;
while ($row = mysql_fetch_assoc($sql)) {

<p>$i. <?php echo $row['comment'] ?></p>

<div class="border"></div>

$i++;
}

How could I do to not output <div class="border"></div>under the last comment?

我怎么能<div class="border"></div>在最后一条评论下不输出?

回答by Tyler Carter

$sql = mysql_query("SELECT * FROM comments WHERE user = 1");
$number = mysql_num_rows($sql);
$i = 1;
while ($row = mysql_fetch_assoc($sql)) {

   echo '<p>' . $i . $row['comment'] . '</p>';

   if ($i < $number)
   {
       echo '<div class="border"></div>';
   }

   $i ++;
}

Using WebDevHobo's suggestion.

使用 WebDevHobo 的建议。

回答by Tyler Carter

$sql = mysql_query("SELECT * FROM comments WHERE user = 1");
$output = array ();
while ($row = mysql_fetch_assoc($sql)) {
  $output[] = $row['comment'];
}
echo join('<div class="border"></div>', $output);

回答by KdgDev

$number = mysql_num_rows($sql);

This will tell you how many rows will be returned. Based on that, you can make sure that the last on does not have the DIV.

这将告诉您将返回多少行。基于此,您可以确保最后一个没有 DIV。

回答by Alex L

$sql = mysql_query("SELECT * FROM comments WHERE user = 1");

$i = 1;
while ($row = mysql_fetch_assoc($sql)) {

$out_data[] = "<p>$i {$row['comment']} </p>";

$i++;
}

$divider = '<div class="border"></div>';
$output = implode ($divider, $out_data);

echo $output;

回答by ggranum

A general answer to this type of problem, and using Javascript because it's easy to throw in a console and play with:

此类问题的一般答案,并使用 Javascript,因为它很容易放入控制台并使用:

var count = 0;
var foo = 3;
while( count < 3 ) {
    console.log("Header - " + count);
    console.log("Body - "+ count);
    console.log("Footer - " + count);
    count++;
}

This will print:

这将打印:

Header - 0
Body - 0
Footer - 0
Header - 1
Body - 1
Footer - 1
Header - 2
Body - 2
Footer - 2

页眉 - 0
正文 - 0
页脚 - 0
页眉 - 1
正文 - 1
页脚 - 1
页眉 - 2
正文 - 2
页脚 - 2

The case being requested is basically saying "Print a footer on all but the last element."

请求的案例基本上是说“在除最后一个元素之外的所有元素上打印页脚”。

If you consider the second loop iteration as simply a continuation of the first you can see how to do this without needing to find the total number of records - e.g. no need to do a second query for count. More succinctly: when you're stuck trying to figure out how to do something using a loop (or recursion) you should try actually writing out what your loop does, but without actually looping - e.g. copy and paste the loop block at least three times.

如果您将第二次循环迭代视为第一次循环迭代的简单延续,您可以看到如何执行此操作而无需查找记录总数 - 例如,无需对计数进行第二次查询。更简洁:当你在试图弄清楚如何使用循环(或递归)做某事时,你应该尝试实际写出你的循环做什么,但不实际循环 - 例如复制和粘贴循环块至少三遍.

Rather than do that here, I'm just going to finish with the answer, and leave the derivation to the reader :~)

而不是在这里做,我只是要完成答案,并将推导留给读者:~)

var count = 0;
var foo = 3;
while( count < 3 ) {
    if( count > 0 ) {
        console.log("Footer - " + (count - 1));
    }
    console.log("Header - " + count);
    console.log("Body - "+ count);
    count++;
}

回答by Scott

$rslt = mysql_query("SELECT * FROM comments WHERE user = 1");

$i = 1;
if ($row = mysql_fetch_assoc($rslt)) {
  echo '<p>'. $i . ' '. $row['comment'] . '</p>';
  $i++;
  while ($row = mysql_fetch_assoc($rslt)){
    echo '<div class="border"></div>';
    echo '<p>'. $i . ' ' . $row['comment'] . '</p>';
    $i++;
  } // end while
} // end if

Avoids needing to know the number of rows. Executes the if statement only once instead of each loop. The HTML and php were kind of messy and inconsistent, so I just assumed the whole block was within php tags. Obviously, open and close the php tag as you see fit.

避免需要知道行数。只执行一次 if 语句而不是每个循环。HTML 和 php 有点混乱和不一致,所以我只是假设整个块都在 php 标签内。显然,按照您认为合适的方式打开和关闭 php 标签。

This is largely a style issue, but I decided that the variable name $sql was a bit misleading, as it is often and commonly used to hold the string of the sql statement to be executed, I therefore changed the variable name to $rslt.

这在很大程度上是一个样式问题,但我认为变量名 $sql 有点误导,因为它经常和常用来保存要执行的 sql 语句的字符串,因此我将变量名更改为 $rslt。