senses-designertravelv3 /application/libraries/Getcsv.php

Language PHP Lines 115
MD5 Hash f0f55ad28b39f9cdbe6cf359a5976f24 Estimated Cost $1,955 (why?)
Repository https://bitbucket.org/matyhaty/senses-designertravelv3 View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?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
Back to Top