从cakephp删除多个记录 [英] Delete Multiple Record from cakephp

查看:65
本文介绍了从cakephp删除多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cakephp的新手。我想从复选框中删除多个记录,但我不知道要使用它。以下是我发布的代码,但不是复选框的值。

I am new in cakephp. I want to delete multiple record from checkbox but i did not know to use it. below is my code it post but not checkbox value.

<?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();?>


推荐答案

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'));
    }

这篇关于从cakephp删除多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆