2017-12-27 24 views
0

나는 cakephp에 초보자입니다. existing_question.ctp라는 체크 박스 폼을 생성하고 배열에 저장했습니다. 그러나 print_r을 시도 할 때 어레이가 정말 이상하게 나옵니다. this처럼 나옵니다.CakePHP 3.5 체크 박스 배열

existing_question.ctp는

<?php 

/** 
* @var \App\View\AppView $this 
* @var \App\Model\Entity\Question[]|\Cake\Collection\CollectionInterface 
$questions 
*/ 
use Cake\ORM\TableRegistry; 
?> 
<nav class="large-3 medium-4 columns" id="actions-sidebar"> 
    <ul class="side-nav"> 
    <li class="heading"><?= __('Actions') ?></li> 
    <li><?= $this->Html->link(__('Courses'), ['controller' => 'Courses', 
    'action' => 'index']) ?></li> 
</ul> 
</nav> 
</nav> 
<div class="questions form large-9 medium-8 columns content"> 
<fieldset> 
    <legend><?= __('Add Question') ?></legend> 
    <table cellpadding="0" cellspacing="0"> 
    <thead> 
     <tr> 
      <th scope="col"><?= $this->Paginator->sort('checked') ?></th> 
      <th scope="col"><?= $this->Paginator->sort('id') ?></th> 
      <th scope="col"><?= $this->Paginator->sort('question') ?></th> 
      <th scope="col"><?= $this->Paginator->sort('marks') ?></th> 
     </tr> 
    </thead> 
    <tbody> 


    <?php echo $this->Form->create($question) ?> 
     <?php foreach ($questions as $question): ?> 
     <tr> 
      <td><?= $this->Form->checkbox('id[].', array('value' => 
    $question['id'], 'name' => 'Question[id][]', 'checked'=>false))?></td> 
      <td><?= $this->Number->format($question->id) ?></td> 
      <td><?= h($question->question) ?></td> 
      <td><?= $this->Number->format($question->marks) ?></td> 
     </tr> 
     <?php endforeach; ?>     
    </tbody> 
    </table> 
    <input type="submit" value="Save">   
    </form> 
    <div class="paginator"> 
    <ul class="pagination"> 

     <?= $this->Paginator->first('<< ' . __('first')) ?> 
     <?= $this->Paginator->prev('< ' . __('previous')) ?> 
     <?= $this->Paginator->numbers() ?> 
     <?= $this->Paginator->next(__('next') . ' >') ?> 
     <?= $this->Paginator->last(__('last') . ' >>') ?> 
    </ul> 
    <p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p> 
    <?php //$this->Form->button(__('Submit'), ['action' => 'existingQuestion']) 
     $this->Form->submit('Save'); 
     // $this->Form->end() ?> 
</div> 
</fieldset> 

나는 많은 튜토리얼을 시도했지만이해야처럼이 나던 나온다. 누구든지 나를 도울 수 있습니까 ??? 편집을 할

: 내가 그것을 모두의 this

+0

모양을

<?= $this->Form->checkbox('id[].', array( 'value' => $question['id'], 'name' => 'Question[id][]', 'checked' => false, 'hiddenField' => false )) ?> 

을 :

은 false로 '에 HiddenField'을 설정하여 비활성화 할 수 있습니다? – shahonseven

+0

게시물을 편집했습니다 –

답변

0

처음처럼 배열에 저장하려는 경우 HTML 몇 가지 문제가있다. 폼은 테이블의 자식이 될 수 없으므로 테이블을 폼으로 포장하십시오.

당신은

를 말한다 '에 HiddenField'는 documentation을 읽을 수 있듯이 - 체크 박스와 라디오 버튼의 경우, 기본적으로 숨겨진 입력 요소도 만들어 주 요소와 함께, 을되도록 $ this-> request-> getData() 키는 값을 지정하지 않아도 존재합니다. 체크 박스의 경우 기본값은 0이고 라디오의 경우 버튼은 ''입니다. 당신이 당신의 코드를 검사하는 경우

그래서 당신은 당신이뿐만 아니라 일부 0 값을 받고 왜이

<input type="hidden" name="Question[id][]" value="0"> 
<input type="checkbox" name="Question[id][]" value="2"> 

같은 것을 볼 수 있습니다. 그래서 최종 코드는 예상되는 결과는 무엇입니까이

<?php 

/** 
* @var \App\View\AppView $this 
* @var \App\Model\Entity\Question[]|\Cake\Collection\CollectionInterface 
$questions 
*/ 

use Cake\ORM\TableRegistry; 

?> 
<nav class="large-3 medium-4 columns" id="actions-sidebar"> 
    <ul class="side-nav"> 
     <li class="heading"><?= __('Actions') ?></li> 
     <li><?= $this->Html->link(__('Courses'), ['controller' => 'Courses', 
       'action' => 'index']) ?></li> 
    </ul> 
</nav> 
</nav> 
<div class="questions form large-9 medium-8 columns content"> 
    <fieldset> 
     <legend><?= __('Add Question') ?></legend> 
     <?= $this->Form->create($question) ?> 
     <table cellpadding="0" cellspacing="0"> 
      <thead> 
      <tr> 
       <th scope="col"><?= $this->Paginator->sort('checked') ?></th> 
       <th scope="col"><?= $this->Paginator->sort('id') ?></th> 
       <th scope="col"><?= $this->Paginator->sort('question') ?></th> 
       <th scope="col"><?= $this->Paginator->sort('marks') ?></th> 
      </tr> 
      </thead> 
      <tbody> 
      <?php foreach ($questions as $question): ?> 
       <tr> 
        <td><?= $this->Form->checkbox('id[].', [ 
          'value' => $question['id'], 
          'name' => 'Question[id][]', 
          'checked' => false 
         ]) ?></td> 
        <td><?= $this->Number->format($question->id) ?></td> 
        <td><?= h($question->question) ?></td> 
        <td><?= $this->Number->format($question->marks) ?></td> 
       </tr> 
      <?php endforeach; ?> 
      </tbody> 
     </table> 
     <?= $this->Form->submit('Save') ?> 
     <?= $this->Form->end() ?> 
     <div class="paginator"> 
      <ul class="pagination"> 
       <?= $this->Paginator->first('<< ' . __('first')) ?> 
       <?= $this->Paginator->prev('< ' . __('previous')) ?> 
       <?= $this->Paginator->numbers() ?> 
       <?= $this->Paginator->next(__('next') . ' >') ?> 
       <?= $this->Paginator->last(__('last') . ' >>') ?> 
      </ul> 
      <p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p> 
      <?php //$this->Form->button(__('Submit'), ['action' => 'existingQuestion']) 
      // $this->Form->submit('Save'); 
      // $this->Form->end() ?> 
     </div> 
    </fieldset> 
</div> 
+0

감사합니다! 그것은 작동합니다! –

+0

안녕하세요, 내 문제 [여기] (https://stackoverflow.com/questions/47988273/save-data-to-another-model-cakephp-3-5)를 살펴 보도록 도움을 요청할 수 있습니다. 그 링크를 보느라 소중한 시간을 할애 해 주시면 정말 고맙습니다. –