蛋糕php中的多个复选框

时间:2022-04-20 10:06:01

I have a list of bookings and I want to delete them using multiple checkboxes. Here is my code:

我有一个预订列表,我想使用多个复选框删除它们。这是我的代码:

foreach ($bookings as $booking): ?>
    <tr>
        <td><?php echo h($booking['Booking']['first_name']); ?>&nbsp;</td>
        <td><?php echo h($booking['Booking']['surname']); ?>&nbsp;</td>
        <td><?php echo h($booking['Booking']['created']); ?>&nbsp;</td>
        <td><?php echo $this->Form-> checkbox('Bookings.ID.['.$booking['Booking']['ID'].']', 
        array('value' => $booking['Booking']['ID']));?></td>    
    </tr>
<?php endforeach; ?>

and in my controller I use this function to delete the selected bookings:

在我的控制器中,我使用此功能删除所选的预订:

public function deletebooking(){

    $bookings = $this->Booking->find('all');
    $this->set('bookings',$bookings);
    if(!empty($this->data)){
        foreach($this->data[Bookings] as $key => $value){

            if($value != 0){
                $this->Booking->delete($value);
                $this->redirect(array('action' => 'index'));
            }

        }
    }

}

Can anyone tell me why it is not working?

谁能告诉我它为什么不起作用?

1 个解决方案

#1


1  

Change your input field to the following: foreach ($bookings as $booking): ?>

将输入字段更改为以下内容:foreach($ bookings as $ booking):?>

<td>
   <?php echo $this->Form->checkbox('bookingids', 
                                      array(
                                        'value' => $booking['Booking']['ID'],
                                        'name' => 'data[Booking][bookingids][]',
                                       ));?>
</td> 

Notice the empty [] in the name value. This will create a new index of that array. In your controller you would access it like this:

注意名称值中的空[]。这将创建该数组的新索引。在您的控制器中,您可以像这样访问它:

   if(!empty($this->data)) :
        foreach($this->data['Bookings']['bookingids'] as $key => $value):
                $data = array();
                $data['Booking']['id'] = $value;
                $this->Booking->delete($data);
        endforeach;
        $this->redirect(array('action' => 'index'));
    endif;

#1


1  

Change your input field to the following: foreach ($bookings as $booking): ?>

将输入字段更改为以下内容:foreach($ bookings as $ booking):?>

<td>
   <?php echo $this->Form->checkbox('bookingids', 
                                      array(
                                        'value' => $booking['Booking']['ID'],
                                        'name' => 'data[Booking][bookingids][]',
                                       ));?>
</td> 

Notice the empty [] in the name value. This will create a new index of that array. In your controller you would access it like this:

注意名称值中的空[]。这将创建该数组的新索引。在您的控制器中,您可以像这样访问它:

   if(!empty($this->data)) :
        foreach($this->data['Bookings']['bookingids'] as $key => $value):
                $data = array();
                $data['Booking']['id'] = $value;
                $this->Booking->delete($data);
        endforeach;
        $this->redirect(array('action' => 'index'));
    endif;