2017-04-17 146 views
2

我想按'Sr.排序表格。否“以降序排列。我不知道如何,因为它不是从数据库,但PHP生成。CakePHP管理面板订单

<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

你能告诉我们$ posts是在哪里定义的吗? –

回答

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')));