2017-12-27 22 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> 

私は非常に多くのチュートリアルを試みたが、それが必要のようにそれはdoesntのが出てきます。誰も私を助けることができます???

EDITED: 私はそれがすべてのthis

+0

のようになります。期待される結果とは何ですか? – shahonseven

+0

投稿を編集しました –

答えて

0

まずような配列に格納したい、あなたのHTMLは、いくつかの問題があります。フォームをテーブルの子にすることはできませんので、フォームでテーブルをラップしてください。

あなたがdocumentationを読むことができるとして、それは

を言う「のHiddenField」 - チェックボックスとラジオボタンの場合は、デフォルトでは、隠された 入力要素も作成され、メイン要素と一緒に、そのよう $ this-> request-> getData()のキーは、値 が指定されていなくても存在します。チェックボックスの場合、デフォルト値は0に設定され、ラジオの場合は ボタンが ''に設定されます。

あなたのコードを調べるのであれば、あなたはあなたにもいくつかの0の値を取得している理由です。この

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

ようなものが表示されます。

これはfalseに「のHiddenField」に設定することで無効にすることができます。

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

だから、最終的なコードは、この

<?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)を見てもらえますか?私は本当にあなたがそのリンクを見て貴重な時間を過ごすことができれば感謝します。 –

関連する問題