Welcome Guest, Not a member yet? Register   Sign In
Codeigniter CSV file undefined index
#1

I'm trying to display the content of CSV file, using Codeigniter.
I used a library CSVReader (found here https://github.com/alemohamad/ci-csv-rea...Reader.php)
I got this error message :
A PHP Error was encountered Severity: Notice Message: Undefined index: head2

Here is my code. Please tell me what is wrong. Thanks

-- File contents

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


--Controller


Code:
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);
    }


--View

Code:
<!-- 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>
Reply
#2

You are using ; as an separator. That script are based on ,.

https://github.com/alemohamad/ci-csv-rea...er.php#L14
Reply
#3

(05-30-2020, 05:29 AM)jreklund Wrote: You are using ; as an separator. That script are based on ,.

https://github.com/alemohamad/ci-csv-rea...er.php#L14


Many Thanks!!!
You saved my day.
I changed the separator in CSVReader Below is the final code.


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

/**
* CSVReader Class
*
* Allows to retrieve a CSV file content as a two dimensional array.
* Optionally, the first text line may contains the column names to
* be used to retrieve fields values (default).
*/
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

    /**
    * Parse a file containing CSV formatted data.
    *
    * @access    public
    * @param    string
    * @param    boolean
    * @return    array
    */
    function parse_file($p_Filepath, $p_NamedFields = true)
    {
        $content = false;
        $file = fopen($p_Filepath, 'r');

        if ($p_NamedFields) {
            $this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
        }

        while ( ($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) {
            if ( $row[0] != null ) { // skip empty lines
                if ( !$content ) {
                    $content = array();
                }

                if ( $p_NamedFields ) {
                    $items = array();

                    foreach ( $this->fields as $id => $field ) {
                        if ( isset($row[$id]) ) {
                            $items[$field] = $row[$id];   
                        }
                    }
                    $content[] = $items;
                } else {
                    $content[] = $row;
                }
            }
        }

        fclose($file);
        return $content;
    }

}

/* End of file CSVReader.php */
Reply




Theme © iAndrew 2016 - Forum software by © MyBB