从cakephp中删除多条记录
我是cakephp的新手。我想从复选框中删除多条记录,但我不知道如何使用它。下面是我的代码,但不是复选框值从cakephp中删除多条记录,cakephp,Cakephp,我是cakephp的新手。我想从复选框中删除多条记录,但我不知道如何使用它。下面是我的代码,但不是复选框值 <?php echo $this->Form-enter code here>create(array('action' => 'deleteall'));?> <?php echo $this->Html->Link('Delete',array('action' => 'deleteall'),array('confirm' =&
<?php echo $this->Form-enter code here>create(array('action' => 'deleteall'));?>
<?php echo $this->Html->Link('Delete',array('action' => 'deleteall'),array('confirm' => 'Are you sure you want to delete selected record?'));?>
<div class="row">
<?php echo $this->Session->flash();?>
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">Subjects</div>
<div class="table-responsive">
<table class="table">
<tr>
<th><?php echo $this->Form->input('checkbox', array('type' => 'checkbox','name'=>'selectAll','label'=>false,'id'=>'selectAll','hiddenField'=>false));?></td>
<th>S.No.</td>
<th>Subject Name</td>
<th>Action</td>
<th>Question Bank Account</td>
</tr>
<?php foreach ($subject as $post): ?>
<tr>
<td><?php echo $this->Form->checkbox('subject',array('value' => $post['Subject']['subject_id'],'name' => "data['Subject']['subject_id'][]",));?></td>
<td><?php echo $post['Subject']['subject_id']; ?></td>
<td><?php echo $post['Subject']['subject_name']; ?></td>
<td><a data-toggle="modal" data-href="<?php echo $this->Html->url(array('action'=>'edit', $post['Subject']['subject_id']));?>"><span class="glyphicon glyphicon-pencil"></span> Edit</a></td>
<td><?php echo $post['Subject']['subject_id']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($post); ?>
</table>
</div>
</div>
</div>
</div>
<?php
echo $this->Form->end();?>
学科
没有。
主题名称
行动
问题银行账户
在视图中使用subjects\u\u删除[]
作为变量名,这将生成一个包含服务器上ID的数组
<td><?php echo $this->Form->checkbox('subjects_to_delete[]',array('value' => $post['Subject']['subject_id'],'name' => "data['Subject']['subject_id'][]",));?></td>
使用index.ctp此代码
在控制器中添加代码
公共函数deleteall()
{
如果($this->request->is('post'))
{
foreach($key=>value)的($this->data['Subject']['Subject\u id']as$key=>value)
{
$this->Subject->delete($value);
}
$this->Session->setFlash('您的主题已被删除');
}
$this->redirect(数组('action'=>'index'));
}
注意(8):未定义索引:如果您的模型是主题
,则主题应为$this->request->data['Subject']['subjects\u to\u delete']
。您可以var\u dump($this->request->data)
查看您现在可能已经解决了这个问题,但我也遇到了同样的问题。数据的名称中不需要单引号,cakephp就是这样格式化的,比如'name'=>data[Subject][Subject\u id][]
然后在controller$this->request->data[Subject][Subject\u id][]
$subjectsToDelete = $this->request->data['subjects_to_delete'];
$this->Subject->deleteAll(array('id' => $subjectsToDelete));
Use index.ctp this code
<td><?php echo $this->Form->checkbox(false,array('value' => $post['Subject']['subject_id'],'name'=>'data[Subject][subject_id][]'));?></td>
Add code in controller
public function deleteall()
{
if ($this->request->is('post'))
{
foreach($this->data['Subject']['subject_id'] as $key => $value)
{
$this->Subject->delete($value);
}
$this->Session->setFlash('Your Subject has been deleted.'));
}
$this->redirect(array('action' => 'index'));
}