开发者

cakephp foreach sort with titles

开发者 https://www.devze.com 2023-03-03 22:46 出处:网络
Hello i\'m started to learning cakephp framework.And just for start i wanted to write some simple game.

Hello i'm started to learning cakephp framework.And just for start i wanted to write some simple game.

I have user controller wited already (not finished but works :D) and now i started to write equipment. But i stuck at the moment i'm trying to sort foreach by type (helmet, armor, shield etc) and now script output table like this:

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

But i wanna to make it like this:

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

}

And equipments/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>

Can anyone help me?


You can add a 'group' option to your 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',
      ),
    )
  )
);

Hopefully you've actually got a Model for those types, and can use that model instead of those Equipment.type values, something like EquipmentType.name would be useful in your view. If you had that, then you'd be able to output a new heading row each time the EquipmentType.id changed.


I I'm understanding you correctly, you'd like to be able to sort your table of data by the titles in the table head tags.

If this is the case, I'd suggest using cake's built in paginator.

Your controller should look like:

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

}

Then in your 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; ?>

Using the paginator this way will generate links in your table headers that will automatically sort the data coming out of the db.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号