Example column in sql:
id|viewed
1|1000
35|500
47|1200
79|700
84|300
I use "SELECT * FROM table ORDER BY viewed desc", the result I get:
47|1200
1|1000
79|700
35|500
84|300
But I want to add "number order" by Top id ( like ranking ) in PHP, example text:
So what I need to add these top with PHP guys, I think will use $i++ to count number or something like this in php ?
Increment a variable during the loop that prints the results.
$i = 1;
while ($row = $stmt->fetch()) {
echo "ID {$row['id']} is $i<br>";
$i++;
}
Something like this?
$query = $db->query("SELECT * FROM table ORDER BY viewed desc");
$rows = $query->fetchAll(PDO:FETCH_ASSOC);
count = count($rows);
echo '<ul>';
for ($x = 0; $x < count; $x++) {
echo '<li>ID ' + $rows[$x]['id'] + ' is ' + $x + '.</li>'
}
echo '</ul>';
You can do it from the sql query itself :
May be you should try this:
set @row_num = 0;
SELECT @row_num := @row_num + 1 AS number,id,viewed FROM `table` ORDER BY viewed desc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.