0

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

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

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

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

        }
    }

}

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

4

1 回答 1

1

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

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

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

   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;
于 2012-12-15T11:29:36.330 回答