cakephp foreach排序标题

时间:2011-05-08 13:21:04

标签: php cakephp foreach

您好 我开始学习cakephp framework.And刚开始我想写一些简单的游戏。

我已经安装了用户控制器(未完成但工作: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

Mine 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());
        }
    }

}

和devices / 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>

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

您可以在find()...

中添加“群组”选项
$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改变时你都可以输出一个新的标题行。

答案 1 :(得分:0)

我正确理解您,您希望能够通过表格标签中的标题对数据表进行排序。

如果是这种情况,我建议使用cake的内置分页器。

您的控制器应如下所示:

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

}

然后在你的views / 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; ?>

以这种方式使用paginator将在表头中生成链接,这些链接将自动对来自db的数据进行排序。