Delete multiple rows by ticking Checkbox

3.7k views Asked by At

How do I delete multiple rows by checking a checkbox? I have a table of records, and I'd like to to delete multiple rows at once.

This is my my deleteselected function:

UPDATED

 public function order() {


if(!empty($this->data)) {
        //If at lesst 1 check box is selected
        if (isset($this->data) && !empty($this->data['order_id'])) {
            $selectedReferences = $this->data['order_id'];

            $flagReferenceAdded = false;
            foreach ($selectedReferences as $singleReference) {
                //NEW DELETE
                $this->Order->id = $singleReference;
                $this->Order->delete();
                //NEW DELETE
            }
            //After delete
            $this->Session->setFlash(
                __('Your All record Are deleted.')
            );
            return $this->redirect(array(''));
        }      
        else{
            echo "Select at least 1 ORDER.";
        }  
}

and in my view.

  <?php
         echo $this->Form->create('Order',array('url' => array('controller' => 'admin', 'action' => 'order'))); ?>
        <table class="table table-bordered table-hover">

                    <th>Name</th>
                    <th>Email</th>
                    <th>phone</th>
                    <th>view</th>
                    <th>delete</th>
            <thead>
                </tr>
            </thead>
            <tbody>

                <?php foreach ($order as $orders): ?>
                <tr>
                  <td><?php echo $orders['Order']['varfullname']; ?></td>
                    <td><?php echo $orders['Order']['varemailid']; ?></td>
                    <td><?php echo $orders['Order']['varphone']; ?></td>
                     <td>
<?php echo $this->Html->link('',
array('controller' => 'orders', 'action' => 'view', $orders['Order']['id']), array('title'=>'VIEW','class' => 'fa fa-search'));?>

</td>
  <td><input type="checkbox" name="order_id[]" value ="<?php echo $orders['Order']['id'];?>" hiddenField = "false">
                </td>
                </tr>

                <?php endforeach; ?>

            </tbody>
        </table>

</br>

          <?
echo $this->Form->end('Deleteselected'); ?> 
    </div>
</div>
</br>

</div>
<!-- /.row -->

after using below answer my delete selected working fine enter image description here

1

There are 1 answers

9
Pratik Joshi On BEST ANSWER

Use in ctp

//in ctp Along with EACH record use Checkbox integration . Dont use Checkbox code u used.

<input type="checkbox" name="order_id[]" value ="<?php echo $referenceSingle['Order']['id'];?>" hiddenField = "false">

//In controller

if(!empty($this->data)) {
        //If at lesst 1 check box is selected
        if (isset($this->data) && !empty($this->data['order_id'])) {
            $selectedReferences = $this->data['order_id'];

            $flagReferenceAdded = false;
            foreach ($selectedReferences as $singleReference) {
                //NEW DELETE
                $this->Order->id = $singleReference;
                $this->Order->delete();
                //NEW DELETE
            }
            //After delete
            $this->Session->setFlash(
                __('Your All record Are deleted.')
            );
            return $this->redirect(array(''));
        }      
        else{
            echo "Select at least 1 ORDER.";
        }  
}

UPDATE

Use Above Code in your Index method itself ,Dont create deleteAll method.

OR Make <form action TO deleteAll

LIKE
echo $this->Form->create('searchCourse',array('url' => array('controller' => 'Courses', 'action' => 'index')));

UPDATE For proper Workflow refer : enter image description here ALWAYS do paperwork and Algorithm ,Before Coding SIR.