2011-05-08 21 views
1

你好 我開始學習cakephp framework.And剛開始我想寫一些簡單的遊戲。cakephp與標題的foreach排序

我有用戶控制器wited已經(不完成,但工作:D),現在我開始寫設備。但我停留在那一刻我試圖按類型排序(頭盔,盔甲,盾牌等)的foreach現在腳本輸出表是這樣的:

Id Owner Name     Status Type Cost 
1 23  Krasnoludzka Salada  B  H  100 
2 23  Jakieś spodnie   B  L  10 
3 23  Zbroja     B  A  123 

但我想使它像這樣:

Id Owner Name     Status Type Cost 
Helmets: 
1 23  Krasnoludzka Salada  B  H  100 
4 23  Smocza Salada   B  H  100 
Legs: 
2 23  Jakieś spodnie   B  L  10 
Armors: 
3 23  Zbroja     B  A  123 

礦equipments_controller.php:

<?php 
class EquipmentsController extends AppController { 

    var $name = 'Equipments'; 
    var $helpers = array('Html', 'Form'); 

    function index() { 
     $this->set('equipments', $this->Equipment->find('all', array('conditions' => array('owner='.$this->Session->read('Auth.User.id'), 'status=\'B\'')))); 
     //$this->set('equipments', $this->Equipment->find('owner='.$this->Session->read('Auth.User.id'))); 
    } 

    function view ($id = null) { 
     $this->Equipment->id = $id; 
     $owner = $this->Equipment->read('owner'); 
     if ($owner['Equipment']['owner']==$this->Session->read('Auth.User.id')) { 
      $this->redirect(array('controller' => 'equipments', 'action' => 'index')); 
      $this->Session->setFlash('To nie twój przedmiot!'); 
     } else { 
     $this->set('equipment', $this->Equipment->read()); 
     } 
    } 

} 

和設備/ index.ctp:

<!-- File: /app/views/news/index.ctp (edit links added) --> 
<h1>Plecak</h1> 
<table> 
    <tr> 
     <th>Id</th> 
     <th>Owner</th> 
     <th>Name</th> 
     <th>Status</th> 
     <th>Type</th> 
     <th>Cost</th> 
    </tr> 

<!-- Here's where we loop through our $news array, printing out news info --> 

<?php foreach ($equipments as $equipment): ?> 
    <tr> 
     <td><?php echo $equipment['Equipment']['id']; ?></td> 
     <td><?php echo $equipment['Equipment']['owner']; ?></td> 
     <td><?php echo $equipment['Equipment']['name']; ?></td> 
     <td><?php echo $equipment['Equipment']['status'];?></td> 
     <td><?php echo $equipment['Equipment']['type']; ?></td> 
     <td><?php echo $equipment['Equipment']['cost']; ?></td> 
    </tr> 
<?php endforeach; ?> 

</table> 

任何人都可以幫我嗎?

回答

0

我正確理解你,你希望能夠通過表頭標籤中的標題對你的數據表進行排序。

如果是這種情況,我建議使用蛋糕建在paginator。

你的控制器看起來應該像:

class EquipmentsController extends AppController { 

    var $name = 'Equipments'; 
    var $helpers = array('Html', 'Form'); 

    public $paginate = array(
     'Equipment' => array(
      'limit' => 25, 
      'order' => array(
       'Equipment.id' => 'ASC' 
      ) 
     ) 
    ); 

    function index() { 
     $owner = $this->Session->read('Auth.User.id'); 
     $equipments = $this->paginate('Equipment', array(
      'Equipment.owner' => $owner, 
      'Equipment.status' => 'B' 
     )); 
     $this->set(compact('equipments')); 
    } 

} 

然後在你的意見/設備/ index.ctp:

<!-- File: /app/views/news/index.ctp (edit links added) --> 
<h1>Plecak</h1> 
<table> 
    <tr> 
     <th><?=$this->Paginator->sort('Id', 'Equipment.id')?></th> 
     <th><?=$this->Paginator->sort('Owner', 'Equipment.owner')?></th> 
     <th><?=$this->Paginator->sort('Name', 'Equipment.name')?></th> 
     <th><?=$this->Paginator->sort('Status', 'Equipment.status')?></th> 
     <th><?=$this->Paginator->sort('Type', 'Equipment.type')?></th> 
     <th><?=$this->Paginator->sort('Cost', 'Equipment.cost')?></th> 
    </tr> 

<!-- Here's where we loop through our $news array, printing out news info --> 

<?php foreach ($equipments as $equipment): ?> 
    <tr> 
     <td><?php echo $equipment['Equipment']['id']; ?></td> 
     <td><?php echo $equipment['Equipment']['owner']; ?></td> 
     <td><?php echo $equipment['Equipment']['name']; ?></td> 
     <td><?php echo $equipment['Equipment']['status'];?></td> 
     <td><?php echo $equipment['Equipment']['type']; ?></td> 
     <td><?php echo $equipment['Equipment']['cost']; ?></td> 
    </tr> 
<?php endforeach; ?> 

使用分頁程序這種方式會產生在你的表頭鏈接,會自動對來自數據庫的數據進行排序。

+0

哦不,他們應該只按類型排序,但我需要在每種類型之前添加標題 – KryQ 2011-05-08 18:14:17

1

您可以添加一個「組」選項,你發現()...

$this->set(
    'equipments', 
    $this->Equipment->find(
    'all', 
    array(
     'conditions' => array(
     'Equipment.owner' => $this->Session->read('Auth.User.id'), 
     'Equipment.status' => 'B' 
    ), 
     'group' => array(
     'Equipment.type' 
    ), 
     'order' => array(
     'Equipment.type', 
     'Equipment.name', 
    ), 
    ) 
) 
); 

希望你實際上已經得到了這些類型的模型,並可以使用該模型來代替那些Equipment.type的值,類似於EquipmentType.name在您的視圖中會很有用。如果你有這個問題,那麼每當EquipmentType.id改變時,你都可以輸出一個新的標題行。

+0

嗯,我感覺很抱歉,但我真的沒有那個安ser者:( 如何設置這個模型? – KryQ 2011-05-08 14:26:08

+0

不知道我完全理解你的評論,但我建議的代碼應該用在你的index()函數中,替換你已經擁有的$ this-> set(),我剛剛添加了一個group子句,並且稍微清理了一些條件我的例子。 – ianmjones 2011-05-09 12:05:28