Skip to main content

loop through CSV and convert to array

12th October, 2022

Updated: 12th October, 2022

    
                   if($_POST)
                  {
                         $p = $_POST;
                         $f = $_FILES[ 'txt_exported_csv'];
    
                          if(isset ($p['txt_target_learner_id' ]) && is_numeric($p['txt_target_learner_id' ]) && isset($_FILES[ 'txt_exported_csv'])) {
                          echo '<pre>';
                               $row = 0;
                                if (($handle = fopen($_FILES['txt_exported_csv' ]['tmp_name' ], "r" )) !== FALSE) {
                                       while (($data = fgetcsv($handle, 5000, "\n" , '"')) !== FALSE ) {
                                             $num = count($data);
                                             $row++;
                                              for ($c=0; $c < $num; $c++) {
                                                     if($row != 1) {
                                                           print_r($this->csv_string_to_array($data[$c]));
                                                            echo $data[$c] . "<br />\n";
                                                    }
                                             }
                                      }
                                      fclose($handle);
                               }
    
                         }
    
                          die;
    
                  }
    
                  $this-> _View->assign( "pagetitle", "Import Learner CPD" );
                  $this-> _View->assign( "sitetitle", "CPD" );
                  $this-> _View->assign( "content", $this->_View->fetch("admin/EditLearner.tpl" ));
                  $this-> showList= false;
           }
            private function setSearchCriteria() {   }
    
            public function csv_string_to_array($str){
                  $expr= "/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/" ;
                  $results=preg_split($expr,trim($str));
                   return preg_replace("/^\"(.*)\"$/" ,"$1" ,$results);
           }

    4c540574-a017-4ba2-b606-4b4b0b769067

    Created on: 12th October, 2022

    Last updated: 12th October, 2022

    Tagged With: