如何使用codeiginter限制将数据插入MySql

时间:2016-07-28 03:37:19

标签: php mysql codeigniter

如何使用codeiginter限制将数据插入MySql? 我有10000个数组数据。 但是我想在每个进程中仅限制200个数据。 下一个进程必须从尚未插入的数据中插入。 这是我的控制器代码:

<?php
 foreach($data["result"] as $key) {

    $insert=$this->db->insert('master_mahasiswa_pt',$key );
    if (!$insert && $this->db->error()) {

  echo "Data nim"; echo " "; echo $key['nipd'] ; echo " ";  echo "Sudah Ada"; echo '<br>'; 
} else {
 echo "Data nim"; echo " "; echo $key['nipd'] ; echo " ";  echo "Sudah Masuk"; echo '<br>';
}
}
}
}
?> 

以下是我的数组数据的示例:

Array
(
    [error_code] => 0
    [error_desc] => 
    [result] => Array
        (
            [0] => Array
                (
                    [id_reg_pd] => 35e86b4f-1b8b-407b-b49e-9d125cba2cb5
                    [nipd] => 150015020
                    [id_pd] => b5261e3d-6aed-41e9-a329-1ebe6191d53c
                    [nm_pd] => ACDB
                    [tgl_lahir] => 1996-06-16
                    [id_sms] => 63d9ecdb-4468-4883-a1aa-13ad3f88fce1
                    [fk__sms] => Teknik Informatika                                                              
                    [id_sp] => 89a93cb2-f959-45d3-8a75-f349be529b87
                    [fk__sp] => STMIK 
                    [tgl_masuk_sp] => 2015-08-25
                    [id_jns_daftar] => 1
                    [fk__jns_daftar] => Peserta didik baru
                    [id_jns_keluar] => 
                    [fk__jns_keluar] => 
                    [tgl_keluar] => 
                    [ket] => 
                    [skhun] => 
                    [a_pernah_paud] => 0
                    [a_pernah_tk] => 0
                    [mulai_smt] => 20151
                    [sks_diakui] => 0
                    [jalur_skripsi] => 
                    [judul_skripsi] => 
                    [bln_awal_bimbingan] => 
                    [bln_akhir_bimbingan] => 
                    [sk_yudisium] => 
                    [tgl_sk_yudisium] => 
                    [ipk] => 
                    [no_seri_ijazah] => 
                    [sert_prof] => 
                    [a_pindah_mhs_asing] => 
                    [nm_pt_asal] => 
                    [nm_prodi_asal] => 
                )

        )

)

1 个答案:

答案 0 :(得分:0)

$i = 0;
    foreach ($data["result"] as $key) {
        if ($i < 201) {
            $insert = $this->db->insert('master_mahasiswa_pt', $key);
            if (!$insert && $this->db->error()) {

                echo "Data nim";
                echo " ";
                echo $key['nipd'];
                echo " ";
                echo "Sudah Ada";
                echo '<br>';
            } else {
                echo "Data nim";
                echo " ";
                echo $key['nipd'];
                echo " ";
                echo "Sudah Masuk";
                echo '<br>';
            }
        }
        $i++;
    }