1

从我的角度来看,我有两个数组。我需要插入它们彼此对应的数据。

以下是我的观点

<?php foreach ($seats as $seat):?>

        <tr>
            <td><input type="checkbox" name="seat_id[]" value=<?php echo $seat->seatNumber;?>></td>
            <td><?php echo $seat->seatLabel;?></td>
            <td><input type="hidden" name="seat_label[]" value="<?php echo $seat->seatLabel;?>"></td>
        </tr>
        <?php endforeach;?>

从上面的视图中,seat_id 和 seat_label 携带必须存储在数据库中的值

这是我的控制器

if($this->form_validation->run()){
            $seat_ids = $this->input->post("seat_id[]");
            $seat_labels = $this->input->post("seat_label[]");
            $busNumber = $this->input->post("busNumber");
            $bookingDate = $this->input->post("bookingDate");
            $reportingTime = $this->input->post("reportingTime");
            $departureTime = $this->input->post("departureTime");
            $this->load->model('Queries');
     $insert = $this->Queries->saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime);

这是我的模型

public function saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime){

    foreach($seat_ids as $seat_id )
          foreach($seat_labels as $seat_label ){
    {
        $record = array(
        'seatNumber' => $seat_id, 
        'seatLabel'  => $seat_label,
        'bookingDate' => $bookingDate,
        'reportingTime' => $reportingTime,
        'departureTime' => $departureTime, 
        'busNumber' => $busNumber,
        'seatUse' => 'Enabled',
        'seatStatus' => 'Available',);
        $this->db->insert('schedule', $record);
    }
4

5 回答 5

1

希望对你有帮助 :

使用insert_batch而不是insert这样:

public function saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime)
{

    foreach($seat_ids as $key => $seat_id )
    {
            $record[] = array(
            'seatNumber' => $seat_id, 
            'seatLabel'  => $seat_labels[$key],
            'bookingDate' => $bookingDate,
            'reportingTime' => $reportingTime,
            'departureTime' => $departureTime, 
            'busNumber' => $busNumber,
            'seatUse' => 'Enabled',
            'seatStatus' => 'Available');
    }
    $this->db->insert_batch('schedule', $record);
}

更多信息:https ://www.codeigniter.com/user_guide/database/query_builder.html#inserting-data

于 2018-08-08T14:13:03.877 回答
0

你的代码应该读

 foreach($seat_ids as $seat_id ){
     foreach($seat_labels as $seat_label ){
        $record = array(
        'seatNumber' => $seat_id, 
        'seatLabel'  => $seat_label,
        'bookingDate' => $bookingDate,
        'reportingTime' => $reportingTime,
        'departureTime' => $departureTime, 
        'busNumber' => $busNumber,
        'seatUse' => 'Enabled',
        'seatStatus' => 'Available',);
        $this->db->insert('schedule', $record);
   }
}
于 2018-08-08T13:51:34.370 回答
0

如果您的密钥来自$seat_id$seat_label匹配,则只需一个 foreach 就可以做到这一点:

foreach($seat_ids as $seat_key => $seat_id ) {
    $record = array(
    'seatNumber' => $seat_id, 
    'seatLabel'  => $seat_label[$seat_key],
    'bookingDate' => $bookingDate,
    'reportingTime' => $reportingTime,
    'departureTime' => $departureTime, 
    'busNumber' => $busNumber,
    'seatUse' => 'Enabled',
    'seatStatus' => 'Available',);
    $this->db->insert('schedule', $record);
}
于 2018-08-08T13:59:24.510 回答
0

考虑到您使用 JSON 提交表单数据并维护数组之间的顺序。

您可以尝试:

$i = 0;
foreach($seat_ids as $seat_id )
{
    $record = array(
    'seatNumber' => $seat_id, 
    'seatLabel'  => $seat_labels[$i],
    'bookingDate' => $bookingDate,
    'reportingTime' => $reportingTime,
    'departureTime' => $departureTime, 
    'busNumber' => $busNumber,
    'seatUse' => 'Enabled',
    'seatStatus' => 'Available',);
    $this->db->insert('schedule', $record);
    $i++;
}
于 2018-08-08T14:10:11.017 回答
-2

目前该表单没有座位标签关系。如果您不勾选所有 id,则之后的标签将对应错误的 id(即 id 的发布数组小于标签的数组)

因此,您必须在表单名称中引入必要的关系:

<input type="checkbox" name="seat[{index}][id]" value="{id}">
<input type="hidden" name="seat[{index}][label]" value="{label}">

如果提交,您现在可以检查两个框是否都已勾选

// using $_POST for simplicity
$seats = array_filter($_POST['seat'], function (array $row) {
    return count($row) === 2;
});

现在您有了一个具有相应 ids/labels 的数组,其中只需要一个循环。

编辑:忽略了第二个输入是一个隐藏字段,但这根本不影响问题。

于 2018-08-08T13:59:57.060 回答