/application/libraries/Getcsv.php
PHP | 114 lines | 83 code | 14 blank | 17 comment | 10 complexity | f0f55ad28b39f9cdbe6cf359a5976f24 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
- <?php
- if (!defined('BASEPATH'))
- exit('No direct script access allowed');
- /**
- * TheBizzTech
- *
- * An open source library built for Codeigniter to read CSV files into associated arrays
- *
- * @author Jason Michels
- * @link http://thebizztech.com
- */
- class Getcsv
- {
- private $file_path = "";
- private $handle = "";
- public function set_file_path($file_path)
- {
- $this -> file_path = $file_path;
- return $this;
- }
- private function get_handle()
- {
- $this -> handle = fopen($this -> file_path, "r");
- return $this;
- }
- private function close_csv()
- {
- fclose($this -> handle);
- return $this;
- }
- //this is the most current function to use
- public function get_array()
- {
- $this -> get_handle();
- $row = 0;
- while (($data = fgetcsv($this -> handle, 0, ",")) !== FALSE)
- {
- if ($row == 0)
- {
- foreach ($data as $key => $value)
- {
- $title[$key] = trim($value);
- //this extracts the titles from the first row and builds array
- }
- }
- else
- {
- $new_row = $row - 1;
- //this is needed so that the returned array starts at 0 instead of 1
- foreach ($title as $key => $value)//this assumes there are as many columns as their are title columns
- {
- $result[$new_row][$value] = trim($data[$key]);
- }
- }
- $row++;
- }
- $this -> close_csv();
- return $result;
- }
- // --------------------------------Main Functions Above----------------------------------------------------- //
- //This function is being left in incase I ever need it
- function get_csv_array()
- {
- $row = 0;
- if (($handle = fopen($this -> file_path, "r")) !== FALSE)
- {
- while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
- {
- $final_array[$row] = $data;
- $row++;
- }
- fclose($handle);
- }
- return $final_array;
- }
- //This function is being left in incase I ever need it
- function get_csv_array_headers($headers)
- {
- $row = 0;
- if (($handle = fopen($this -> file_path, "r")) !== FALSE)
- {
- while (($data = fgetcsv($handle, 10000, ",")) !== FALSE)
- {
- //print_r($data);
- foreach ($headers as $h_key => $h_value)
- {
- if ($h_value != 'useless')
- {
- $final_array[$row][$h_value] = $data[$h_key];
- }
- }
- //$final_array[$row] = $data;
- $row++;
- }
- fclose($handle);
- }
- return $final_array;
- }
- } //End of class
- //Here is the end of the getcsv.php class