id | num | text
1 | a | sdfsdf
2 | a | sdfsdf
3 | b | sdfsdf
4 | c | sdfsdf
5 | a | sdfsdf
6 | b | sdfsdf
7 | a | sdfsdf
8 | c | sdfsdf
我查询 num = a 和orderBy ID DESC ,我有:
id | num | text
7 | a | sdfsdf
5 | a | sdfsdf
2 | a | sdfsdf
1 | a | sdfsdf
我想为这个自己的计数器添加:
count | id | num | text
4 | 7 | a | sdfsdf
3 | 5 | a | sdfsdf
2 | 2 | a | sdfsdf
1 | 1 | a | sdfsdf
我使用foreach PHP获取数据:
foreach ($data as $i => $dat) {
echo $dat->id . $dat->num . $dat->text;
}
我想要一些事情:
foreach ($data as $i => $dat) {
echo $i . $dat->id . $dat->num . $dat->text;
}
但$ i反方向
答案 0 :(得分:3)
你可以这样做:
$count = count($data);
foreach ($data as $i => $dat) {
echo ($count - $i) . $dat->id . $dat->num . $dat->text;
}
答案 1 :(得分:2)
在PHP中:
$i = count($data);
foreach ($data as $dat) {
echo $i . $dat->id . $dat->num . $dat->text;
$i--;
}
答案 2 :(得分:2)
您知道,您可以在SQL查询中解决这个问题。这是针对生产Redmine数据库实例(MySQL)运行的示例:
select @curRow := @curRow + 1 AS row_number, issues.*
from issues, (SELECT @curRow := 0) r
where project_id = 7
and tracker_id = 3
order by 1 desc