2011-05-08 6 views
1

안녕하세요 저는 cakephp 프레임 워크를 배우기 시작했습니다. 시작하기 전에 간단한 게임을 만들고 싶었습니다.cakephp foreach 제목이있는 정렬

나는 사용자 컨트롤러가 이미 (아직 끝나지 않았지만 작동한다 : 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

저는 올바르게 이해하고 있습니다. 테이블 헤드 태그의 제목으로 데이터 테이블을 정렬 할 수 있기를 원합니다.

이 경우에는 케이크 조각을 페이징 기능으로 제작하는 것이 좋습니다.

컨트롤러가 같아야합니다 : 귀하의 의견/장비/index.ctp에서 다음

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

} 

: 테이블 헤더에있는 링크를 생성이 방법을 매기기를 사용

<!-- 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; ?> 

이 자동으로 db에서 나오는 데이터 정렬.

+0

없이 그들은 유일한 유형으로 분류한다 오하지만 난 각 유형 – KryQ

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

그럼 내가 기분 미안하기 전에 제목 추가해야하지만 난 정말 anwser :(이 모델을 설정하는 방법 을 얻을 수 있습니까? – KryQ

+0

을하지 I 확인 귀하의 의견을 완전히 이해하지만 내가 제안한 코드는 이미 가지고있는 $ this-> set()을 대체하여 index() 함수에서 사용해야합니다. 방금 그룹 절을 추가하고 약간의 조건을 정리했습니다 내 예. – ianmjones

관련 문제