CakePHP 3.5复选框数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CakePHP 3.5复选框数组相关的知识,希望对你有一定的参考价值。
我是cakephp的新手。我创建了一个名为existing_question.ctp的复选框表单并存储在数组中。但是,当我尝试print_r时,阵列出来真的很奇怪。它像this一样出现。
existing_question.ctp
<?php
/**
* @var AppViewAppView $this
* @var AppModelEntityQuestion[]|CakeCollectionCollectionInterface
$questions
*/
use CakeORMTableRegistry;
?>
<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有一些问题。表单不能是表的子表,因此请按表单包装表。
正如你可以阅读它所说的documentation
'hiddenField' - 对于复选框和单选按钮,默认情况下,还会创建一个隐藏的输入元素以及主元素,这样即使没有指定值,$ this-> request-> getData()中的键也会存在。对于复选框,其值默认为0,单选按钮为''。
因此,如果您检查代码,您将看到类似的内容
<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 AppViewAppView $this
* @var AppModelEntityQuestion[]|CakeCollectionCollectionInterface
$questions
*/
use CakeORMTableRegistry;
?>
<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>
以上是关于CakePHP 3.5复选框数组的主要内容,如果未能解决你的问题,请参考以下文章
CakePHP 3.5:通过Composer安装后无法访问插件类