Codeigniter - 将数据插入MySQL,其中只有一个Input值是数组

时间:2018-05-19 17:54:32

标签: php codeigniter multidimensional-array

我正在学习Codeigniter并且遇到一些动荡。我的一个输入字段收集一组值(来自复选框),其余的是单值。我想以插入行数等于数组中值的数量的方式将数据插入MySQLi。它看起来像:

我的控制器:

 function request_rooms() {     
      if ($_POST) {
        //room is an array or one or more values
        $this->form_validation->set_rules('room[]', 'Select Box', 'trim|required|min_length[1]|max_length[25]'); 
        $this->form_validation->set_rules('guest_id', 'Guest ID', 'trim|required|xss_clean|min_length[4]|max_length[20]');
         $this->form_validation->set_rules('date_in', 'Check-in Date', 'trim|required|exact_length[10]'); 
        $this->form_validation->set_rules('date_out', 'Check-out Date', 'trim|required|exact_length[10]'); 

        if ($this->form_validation->run($this) == FALSE) {
            $somedata = array('errors' => validation_errors());
            $this->session->set_flashdata($somedata);
            require("includes/load_view_reservation.php");
          } else {
               //Prepare data to send to Reservation model
               $data = array( 
               'room_no'  = ?
               'guest_id' = $this->input->post('guest_id'),
               'check_in' = $this->input->post('check_in'),
               'check_in' = $this->input->post('check_in')

             $this->reservation_model->insert_reservation($data);
              }       
       }
}

问题: 我的问题是如何处理房间阵列。我想为要插入数据库的数据创建一个$ data数组。所以假设房间数组输入($ this-> input-> post('room []'))如下:

$room = array(42, 123, 16);

我希望$ data数组如下:

 $data = array
  (
  array(
  "room_no" => "42",
  "guest_id" => "2",
  "date_in" => "2018-05-20",
  "date_out" => "2018-05-27"
  ),
  array(
  "room_no" => "123",
  "guest_id" => "2",
  "date_in" => "2018-05-20",
  "date_out" => "2018-05-27" ),
  array(
  "room_no" => "16",
  "guest_id" => "2",
  "date_in" => "2018-05-20",
  "date_out" => "2018-05-27" )
  );

我有一个微弱的想法,循环可以完成这样的事情,以便数组的room_no部分具有数组中的每个值,而其他值保持不变,但我看不到如何继续。

问题:

如何在Codeigniter中创建如上所述的多维数组?其次,到目前为止,我只使用Codeigniter模型插入(进入MySQL)一个简单的一维数组。如何插入像上面那样的多维数组?请帮忙。

2 个答案:

答案 0 :(得分:1)

看起来不那么难以迭代你的数组

以下内容应该可以胜任。

$room = array(42, 123, 16);

$arrData = [];

foreach($room AS $roomNo)
{
    $arrData[] = [
        'room_no' => $roomNo,
        'guest_id' => $this->input->post('guest_id'),
        'check_in' = $this->input->post('check_in'),
        'check_in' = $this->input->post('check_in')
    ];
}

print_r($arrData);

答案 1 :(得分:1)

希望这会对您有所帮助:

第一种方法 插入个别记录;

//$rooms = $this->input->post('room');
$rooms = array(42, 123, 16);
if ( ! empty($rooms))
{
  foreach($rooms AS $room)
  {
    $data['room_no'] = $room;
    $data['guest_id'] = $this->input->post('guest_id');
    $data['check_in'] = $this->input->post('check_in');

    $this->reservation_model->insert_reservation($data);
  }
}

您的模型方法insert_reservation应该是这样的:

public function insert_reservation($data)
{
   return $this->db->insert('table_name',$data);
}

第二种方法 插入多个(批量)记录;

//$rooms = $this->input->post('room');
$rooms = array(42, 123, 16);
if( ! empty($rooms))
{
   foreach($rooms AS $room)
   {
      $data[] = [
            'room_no' => $room,
            'guest_id' => $this->input->post('guest_id'),
            'check_in' => $this->input->post('check_in'),
        ];
   }
}
//print_r($data);
$this->reservation_model->insert_reservation($data);

在这种情况下,您的模型方法insert_reservation应该是这样的:

public function insert_reservation($data)
{
   return $this->db->insert_batch('table_name',$data);
}

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