Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an easy way to do this. I don't know what people do but i use this</p> <p>This is my csv reader library, Save this in libraries folder as csvreader.php.</p> <pre><code>&lt;?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-&gt;fields = fgetcsv($file, $this-&gt;max_row_size, $this-&gt;separator, $this-&gt;enclosure); $keys_values = explode(',',$this-&gt;fields[0]); $content = array(); $keys = $this-&gt;escape_string($keys_values); $i = 1; while(($row = fgetcsv($file, $this-&gt;max_row_size, $this-&gt;separator, $this-&gt;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-&gt;escape_string($values); for($j=0;$j&lt;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; } } ?&gt; </code></pre> <p>And controller method</p> <pre><code>function readExcel() { $this-&gt;load-&gt;library('csvreader'); $result = $this-&gt;csvreader-&gt;parse_file('Test.csv');//path to csv file $data['csvData'] = $result; $this-&gt;load-&gt;view('view_csv', $data); } </code></pre> <p>And this is view</p> <pre><code>&lt;table cellpadding="0" cellspacing="0" width="100%"&gt; &lt;tr&gt; &lt;td width = "10%"&gt;ID&lt;/td&gt; &lt;td width = "20%"&gt;NAME&lt;/td&gt; &lt;td width = "20%"&gt;SHORT DESCRIPTION&lt;/td&gt; &lt;td width = "30%"&gt;LONG DESCRIPTION&lt;/td&gt; &lt;td width = "10%"&gt;STATUS&lt;/td&gt; &lt;td width = "10%"&gt;PARENTID&lt;/td&gt; &lt;/tr&gt; &lt;?php foreach($csvData as $field){?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $field['id']?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $field['name']?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $field['shortdesc']?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $field['longdesc']?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $field['status']?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $field['parentid']?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php }?&gt; &lt;/table&gt; </code></pre> <p>Note : This will only read a file which exists on server. If the file is needed to be uploaded use <a href="http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html">File Upload Class</a> to upload file and save it to some location on your server then give the path of located file in <code>parse_file</code> method. And everything will work fine.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload