php 回显当前行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3125197/
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
Echo current number of row
提问by Victor Bjelkholm
My $num_rows echos the total row count but I want the current row number.
我的 $num_rows 回显了总行数,但我想要当前的行号。
<?php
$result = mysql_query('SELECT * FROM texts ORDER BY id desc');
while($row = mysql_fetch_array($result)){
$num_rows = mysql_num_rows($result);
?>
<tr class="alt">
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><a href="<?php echo $row['orginal']; ?>">Original</a></td>
<td><a href="#">@English</a></td>
<td><a href="#"><?php echo $num_rows; ?></a></td>
</tr>
<?php
}
?>
Thank you :)
谢谢 :)
回答by BlueBird
why you should not use a increment variable to count the loop turns inside while?
为什么不应该使用增量变量来计算 while 内的循环次数?
$tmpCount = 1;
while() {
$tmpCount ++;
}
Is this helpful to you? or are you expecting the physical row number of from database?
这对你有帮助吗?或者您期望来自数据库的物理行号?
回答by Chris
If you want to just number the rows returned by the query (instead of using the actual ID - $row['id']presumably), you can just increment a number with each row:
如果您只想对查询返回的行进行编号(而不是使用实际 ID -$row['id']大概),您可以只为每一行增加一个数字:
$num_rows = 0;
while ($row = mysql_fetch_array($result)){
$num_rows++;
// ...
回答by Kim Wilson
This works, but when sorting, the number gets locked in and doesn't return to the natural sort. The initial sort will be 1, 2, 3 but when clicking on the column to sort, whatever row is associated with the first sort will stay with it and the new sort will not have the num_rows in order!
这有效,但在排序时,数字被锁定并且不会返回到自然排序。初始排序将是 1、2、3,但是当单击要排序的列时,与第一个排序关联的任何行都将保留在该列中,而新排序将不会按顺序排列 num_rows!

