【问题标题】:How to use two foreach to insert data in database如何使用两个foreach向数据库中插入数据
【发布时间】:2018-08-08 13:46:13
【问题描述】:

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

以下是我的观点

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

【问题讨论】:

  • 所以您基本上希望foreach() 循环并行运行?

标签: php codeigniter codeigniter-3 codeigniter-query-builder


【解决方案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

【讨论】:

  • 先生,请解释一下并添加评论,这是我的要求。
  • 和insert_batch()和insert()有什么区别??
【解决方案2】:

你的代码应该是这样的

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

【讨论】:

    【解决方案3】:

    如果您来自 $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);
    }
    

    【讨论】:

    • 如果不是所有的 id 都打勾,不正确的 id-label 关系怎么办?
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      当前表单没有座位标签关系。如果你没有勾选所有的 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 的数组,其中只需要一个循环。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-14
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-17
        相关资源
        最近更新 更多