Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP 3.5复选框数组_Php_Arrays_Cakephp_Cakephp 3.x - Fatal编程技术网

CakePHP 3.5复选框数组

CakePHP 3.5复选框数组,php,arrays,cakephp,cakephp-3.x,Php,Arrays,Cakephp,Cakephp 3.x,我是cakephp的新手。我创建了一个名为existing_question.ctp的复选框表单并存储在数组中。然而,当我试图打印时,数组显示得非常奇怪。结果是这样的 现有问题.ctp <?php /** * @var \App\View\AppView $this * @var \App\Model\Entity\Question[]|\Cake\Collection\CollectionInterface $questions */ use Cake\ORM\TableRegist

我是cakephp的新手。我创建了一个名为existing_question.ctp的复选框表单并存储在数组中。然而,当我试图打印时,数组显示得非常奇怪。结果是这样的

现有问题.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>

我试过很多教程,但结果都不理想。有人能帮我吗

编辑:
我希望它像

一样存储在数组中首先,HTML有一些问题。表单不能是表的子级,因此请按表单包装表

正如你所看到的,上面写着

“hiddenField”-对于复选框和单选按钮,默认情况下,隐藏 还将创建输入元素以及主元素,以便 $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值

可通过将“hiddenField”设置为false来禁用此功能:

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

最后的代码如下所示

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


预期的结果是什么?我已经编辑了postHi,我可以请你帮我看看我的问题[这里]()。我真的很感激你能花你宝贵的时间来看看这个链接。