Victor Bjelkholm
Victor Bjelkholm

Reputation: 2016

Echo current number of row

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 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 :)

Upvotes: 3

Views: 37456

Answers (3)

Kim Wilson
Kim Wilson

Reputation: 107

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!

Upvotes: 0

Chris
Chris

Reputation: 10435

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++;
  // ...

Upvotes: 6

Muneer
Muneer

Reputation: 7564

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?

Upvotes: 11

Related Questions