我想按'老不'按降序排列。我不确定是怎么回事,因为它不是来自数据库,而是生成了php。
<table>
<tr>
<th>Sr. No</th>
<th>Naslov</th>
</tr>
<?php $counter = 0; foreach($posts as $post) : $counter++; ?>
<tr>
<td><?php echo $counter?></td>
<td><?php echo $this->Html->link($post['Post']['title'], array('controller' => 'Posts', 'action'=> 'view', $post['Post']['id']), array('class' => 'link')); ?></td>
</tr>
<?php endforeach; ?>
<?php unset($post); ?>
</table>
答案 0 :(得分:1)
您需要按以下顺序设置$counter
: -
<?php $counter = count($posts); foreach($posts as $post) : $counter--; ?>
<tr>
<td><?php echo $counter?></td>
<td><?php echo $this->Html->link($post['Post']['title'], array('controller' => 'Posts', 'action'=> 'view', $post['Post']['id']), array('class' => 'link')); ?></td>
</tr>
<?php endforeach; ?>
如果您希望Naslov
(表示帖子)也按降序排列,那么在查询中您必须执行ORDER BY <column name like id> DESC
检查如何应用DESC
: - https://stackoverflow.com/a/9837302/4248328
示例: - $this->Post->find('all', array('order' =>array('Post.id DESC')));