2017-12-27 5 views
0

Ich bin der Neuling zu CakePHP. Ich habe ein Kontrollkästchen Formular namens existing_question.ctp erstellt und im Array gespeichert. Jedoch, als ich versuchte, print_r das Array kommt wirklich komisch. Es kommt wie this heraus.CakePHP 3.5 Checkbox Array

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> 

Ich habe versucht, so viele Tutorials, aber es funktioniert nicht kommt, wie es sollte. Kann mir jemand helfen???

EDITED: Ich will es wie this

+0

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

So aussieht – shahonseven

+0

Ich habe den Beitrag bearbeitet –

Antwort

0

allererst in Array gespeichert werden, hat man HTML einige Probleme. Das Formular kann kein untergeordnetes Element der Tabelle sein, also wickeln Sie die Tabelle nach Formular um.

Wie können Sie die documentation lesen es sagen

‚Hidden‘ - für Kontrollkästchen und Optionsfelder, die standardmäßig ein verstecktes Eingabeelement auch erzeugt wird, zusammen mit dem Hauptelement, so dass die Schlüssel in $ this-> request-> getData() existiert auch ohne einen Wert angegeben. Für Kontrollkästchen ist der Wert standardmäßig 0 und für Radio Schaltflächen auf ''.

Also, wenn Sie Ihren Code überprüfen werden Sie so etwas wie dieses

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

Das ist, warum Sie einige 0 Werte werden immer als gut.

Dies kann durch Setzen 'Hidden' auf false deaktiviert werden: der endgültige Code Was die erwarteten Ergebnisse sind wie diese

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

Danke! Es klappt! –

+0

Hallo, darf ich deine Hilfe fragen, um mein Problem [hier] (https://stackoverflow.com/questions/47988273/save-data-to-another-model-cakephp-3-5) zu sehen. Ich schätze es sehr, wenn Sie Ihre kostbare Zeit damit verbringen, sich diesen Link anzuschauen. –