Home > other >  Echo current number of row
Echo current number of row

Time:02-10

My $num_rows echos the total row count but I want the current row number.

<?php
$result = mysql_query('SELECT * FROM texts ORDER BY id desc');
while($row = mysql_fetch_array($result)){
$num_rows = mysql_num_rows($result);
?>
<tr >
    <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 :)

CodePudding user response:

why you should not use a increment variable to count the loop turns inside while?

$tmpCount = 1;
while() {

   $tmpCount   ;
}

Is this helpful to you? or are you expecting the physical row number of from database?

CodePudding user response:

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:

$num_rows = 0;

while ($row = mysql_fetch_array($result)){
  $num_rows  ;
  // ...

CodePudding user response:

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!

  •  Tags:  
  • Related