CakePHP 3.5 复选框数组

CakePHP 3.5 checkbox array

我是 cakephp 的新手。我创建了一个名为 existing_question.ctp 的复选框表单并存储在数组中。但是,当我尝试 print_r 时,数组的结果真的很奇怪。结果像 this.

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

这样的数组中

首先,你HTML有一些问题。表单不能是 table 的子项,因此请用表单包裹 table。

你可以阅读 documentation 它说的

'hiddenField' - For checkboxes and radio buttons, by default, a hidden input element is also created, along with the main element, so that the key in $this->request->getData() will exist even without a value specified. For checkboxes its value defaults to 0 and for radio buttons to ''.

所以如果你检查你的代码,你会看到类似这样的东西

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

这就是为什么您也得到一些 0 值的原因。

这可以通过将 'hiddenField' 设置为 false 来禁用:

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