Неопределенный индекс файла CSV Codeigniter - PullRequest
0 голосов
/ 27 мая 2020

Я пытаюсь отобразить содержимое CSV-файла с помощью Codeigniter. Я использовал библиотеку CSVReader (здесь https://github.com/alemohamad/ci-csv-reader/blob/master/application/libraries/CSVReader.php). Я получил это сообщение об ошибке:

A PHP Обнаружена ошибка Серьезность: Уведомление: Неопределенный индекс: head2

Вот мой код. Подскажите пожалуйста, что не так. Спасибо

- Содержимое файла

head1;head2
EBBST02;col
EBBST08;lad
EBBST12;vad
EBBST1;saz
EBBST19;xed
EBBSS28;red

- Контроллер

public function import(){
        $data = array();
        $memData = array();
        // If import request is submitted
        if($this->input->post('importSubmit')){
            // Form field validation rules
            $this->form_validation->set_rules('file', 'CSV file', 'callback_file_check');

            // Validate submitted form data
            if($this->form_validation->run() == true){

                // If file uploaded
                if(is_uploaded_file($_FILES['file']['tmp_name'])){
                    // Load CSV reader library
                    $this->load->library('CSVReader');

                    // Parse data from CSV file
                   // $csvData = $this->csvreader->parse_csv($_FILES['file']['tmp_name']);
                    $result =   $this->csvreader->parse_file($_FILES['file']['tmp_name']);
                    $data['csvDatadisp'] =$result; 


            }else{
                $this->session->set_userdata('error_msg', 'Invalid file, please select only CSV file.');
            }
        }
        //redirect('uploadria');
        $this->load->view('chargement/ria.php', $data);
    }

- Просмотр

<!-- Data list table -->
        <table class="table table-striped table-bordered">
            <thead class="thead-dark">
                <tr>
                    <th>Header 1</th>
                   <th>Header 2</th>
                </tr>
            </thead>
            <tbody>
                <?php if(!empty($csvDatadisp)){ foreach($csvDatadisp as $index => $value){ ?>
                <tr>
                    <td><?php echo $value['head1'] ?></td>
                    <td><?php echo $value['head2'] ?></td>
                </tr>
                <?php } }else{ ?>
                <tr><td colspan="5">No row found...</td></tr>
                <?php } ?>
            </tbody>
        </table>

--- Библиотека CSVREADER

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CSVReader {

    var $fields;            /** columns names retrieved after parsing */ 
    var $separator = ';';    /** separator used to explode each line */
    var $enclosure = '"';    /** enclosure used to decorate each field */

    var $max_row_size = 4096;    /** maximum row size to be used for decoding */

    function parse_file($p_Filepath) {

        $file = fopen($p_Filepath, 'r');
        $this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
        $keys_values = explode(',',$this->fields[0]);

        $content    =   array();
        $keys   =   $this->escape_string($keys_values);

        $i  =   1;
        while( ($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) {            
            if( $row != null ) { // skip empty lines
                $values =   explode(',',$row[0]);
                if(count($keys) == count($values)){
                    $arr    =   array();
                    $new_values =   array();
                    $new_values =   $this->escape_string($values);
                    for($j=0;$j<count($keys);$j++){
                        if($keys[$j] != ""){
                            $arr[$keys[$j]] =   $new_values[$j];
                        }
                    }

                    $content[$i]=   $arr;
                    $i++;
                }
            }
        }
        fclose($file);
        return $content;
    }

    function escape_string($data){
        $result =   array();
        foreach($data as $row){
            $result[]   =   str_replace('"', '',$row);
        }
        return $result;
    }   
}
?> 
...