mysql - my php code is only importing the first line of my csv file to the database -


so, have csv file want import database importing first line. dont know why not importing data in csv file. maybe because of line breaks?

sample of csv file:

"tiago" <20>,20,11,sip/11,20,w,2016-03-01 14:33:06,2016-03-01 14:33:09,2016-03-01 14:33:51,45 "claudio" <10>,10,11,sip/11,20,w,2016-03-01 14:35:05,2016-03-01 14:35:07,2016-03-01 14:35:48,43 "hortencio" <11>,11,21,sip/21,20,w,2016-03-01 14:39:55,2016-03-01 14:40:12,2016-03-01 14:40:25,30 "andre" <19>,19,22,22000@default,s,2016-03-01 14:43:22,2016-03-01 14:43:42,2016-03-01 14:43:42,20

my model:

<?php  class csv_model extends ci_model {  var $gallery_path; var $gallery_path_url; function __construct() {     parent::__construct();       $this->gallery_path = realpath(apppath . '../csv');     $this->gallery_path_url = base_url() . 'csv/'; }  function do_upload($data) {         $filename = $data['fileinfo'][0];         $path = $data['fileinfo'][1];         ini_set("auto_detect_line_endings", true);         if (!$this->upload->do_upload('userfile')) {             $error = array('error' => $this->upload->display_errors());              $this->load->view('csv_form', $error);         } else {             $data = array('upload_data' => $this->upload->data());             $this->load->view('csv_success', $data);             $linecount=count(file($path.''.$filename.'.csv'));              $file = fopen($path.''.$filename.'.csv', "r");              $csv = fgetcsv($file, 0, ",", ' ');             fclose($file);               $n = 0;             echo $l=$linecount;             function validatedate($date, $format = 'y-m-d h:i:s'){                  $d = datetime::createfromformat($format, $date);                  return $d && $d->format($format) == $date;             }             for($i = 0; $i <= $l && $n < $l; $i++){                  $insert_csv = array();                  $insert_csv['clid'] = $csv[$n];                 $n++;                 $insert_csv['src'] = $csv[$n];                 $n++;                 $insert_csv['dst'] = $csv[$n];                 $n++;                 $insert_csv['dstchannel'] = $csv[$n];                 $n++;                 if(is_numeric($csv[$n])){                     echo 'yes';                 $insert_csv['numero'] = $csv[$n];                 $n++;                 }else{                 echo"ok";                 $insert_csv['numero'] = " ";}                 if(ctype_alpha($csv[$n])){                 $insert_csv['w'] = $csv[$n];                 $n++;                 }else{                 $insert_csv['w'] = " ";                 echo"ok";}                 $insert_csv['start'] = $csv[$n];                 $n++;                 $insert_csv['answer'] = $csv[$n];                 $n++;                 if(validatedate($csv[$n])){                 $insert_csv['end'] = $csv[$n];                 $n++;                 }else{                 $insert_csv['end'] = " ";}                 if($csv[$n] == 0){                 $insert_csv['duration'] = " ";                 $n++;}else{                 $insert_csv['duration'] = $csv[$n];                 $n++;}                   $data1 = array(                 'clid' => $insert_csv['clid'],                 'src' => $insert_csv['src'],                 'dst' => $insert_csv['dst'],                 'dstchannel' => $insert_csv['dstchannel'],                 'numero' => $insert_csv['numero'],                 'w' => $insert_csv['w'],                     'start' => $insert_csv['start'],                 'answer' => $insert_csv['answer'],                 'end' => $insert_csv['end'],                 'duration' => $insert_csv['duration']);                  $this->db->insert('chamadas', $data1);              }             echo 'success!!!';             $success['success'] = "success";             return $success;          } }   function get_all()     {         return $this->db->get('chamadas')->result_array();     }  } 

you have loop on each line read whole file, otherwise you're reading first line in file

`

$file = fopen($path.''.$filename.'.csv', "r"); $csv = array();   while (($csv = fgetcsv($file, 0, ",", ' ')) !== false)  {    $n[] = $csv; } fclose($file); 

`


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -