user3109875
user3109875

Reputation: 828

How to number each row returned from mysql as they are returned

So What I want to do is to echo 1 for the first row that is returned and two for the 2nd one and so one, Please I just don't know how to phrase this, I don't want to just count rows from mysql table, I want to give each row a number automatically and echo that number.

Please forgive my deprecated code.

$log = mysql_query("SELECT * FROM $table WHERE $columnid = '$id'") or die (mysql_error());
while($row = mysql_fetch_array($log)){
    echo row['name'];
}

Lets assume that the above returns some names. so I want this. 1 : John 2 : Nancy 3 : Dave

I don't want to write those number.

Please take into account ORDER by rate DESC since I'm using that to descent the number by the biggest number of rate.

Upvotes: 0

Views: 66

Answers (2)

aex
aex

Reputation: 85

$log = mysql_query("SELECT * FROM $table WHERE $columnid = '$id'") or die (mysql_error());
$c = 0
while($row = mysql_fetch_array($log)){
    echo row['name'];
    $c = $c + 1;
}
echo "Raw Count = ".$c

Upvotes: 0

mister martin
mister martin

Reputation: 6252

You could do:

$count = 1;
while($row = mysql_fetch_array($log)){
    echo $count . ' ' . row['name'];
    $count++;
}

Or:

echo '<ol>';
while($row = mysql_fetch_array($log)){
    echo '<li>' . row['name'] . '</li>';
}
echo '</ol>';

Upvotes: 3

Related Questions