2017-04-17 253 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')));

相關問題