回声当前行数

Vic*_*olm 2 php mysql

我的$ 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
}
?>
Run Code Online (Sandbox Code Playgroud)

谢谢 :)

Blu*_*ird 10

为什么你不应该使用增量变量来计算循环内部的转数?

$tmpCount = 1;
while() {

   $tmpCount ++;
}
Run Code Online (Sandbox Code Playgroud)

这对你有帮助吗?或者您期望数据库的物理行号?


Chr*_*ris 6

如果您只想对查询返回的行进行编号(而不是使用实际 ID -$row['id']大概),您可以只为每一行增加一个数字:

$num_rows = 0;

while ($row = mysql_fetch_array($result)){
  $num_rows++;
  // ...
Run Code Online (Sandbox Code Playgroud)