如何创建代码签名批处理插入数组

2022-08-30 15:04:07

如何创建一个CI批量插入数组,它类似于

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name' ,
      'date' => 'My date'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name' ,
      'date' => 'Another date'
   )
);

从我的正常数组,这是

Array
(
    [0] => Array
        (
            [track_id] => 
            [camp_id] => 1
            [field_name] => email_title
            [field_value] => sample
        )

    [1] => Array
        (
            [track_id] => 
            [camp_id] => 1
            [field_name] => email_date
            [field_value] => 2013-07-02
        )

    [2] => Array
        (
            [track_id] => 
            [camp_id] => 1
            [field_name] => email_template
            [field_value] => 2
        )
)

答案 1

试试这个:

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name' ,
      'date' => 'My date'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name' ,
      'date' => 'Another date'
   )
);

$this->db->insert_batch('mytable', $data); 

有关更多信息,请阅读此处


答案 2

这是您的数据 在控制器

$dataForDb[0]['track_id']=$data['track_id'];
$dataForDb[0]['camp_id']=$data['camp_id'];
$dataForDb[0]['field_name']=$data['field_name'];
$dataForDb[0]['field_value']=$data['field_value'];

$dataForDb[1]['track_id']=$data['track_id'];
$dataForDb[1]['camp_id']=$data['camp_id'];
$dataForDb[1]['field_name']=$data['field_name'];
$dataForDb[1]['field_value']=$data['field_value'];

$dataForDb[2]['track_id']=$data['track_id'];
$dataForDb[2]['camp_id']=$data['camp_id'];
$dataForDb[2]['field_name']=$data['field_name'];
$dataForDb[2]['field_value']=$data['field_value'];

$fid=$this->model_modelName->functionName($dataForDb);

并将数据作为批量插入插入到模型

$this->db->insert_batch($table, $data);

推荐