如何使用两个foreach在数据库中插入数据



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

以下是我的观点

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

希望这会对您有所帮助:

使用 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

你的代码应该读

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

如果你是$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);
}

考虑到您正在使用 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++;
}

目前,该表格没有座位标签关系。 如果您没有勾选所有 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;
});

现在你有一个带有相应 id/标签的数组,只需要一个循环。

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

相关内容

  • 没有找到相关文章

最新更新