PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Getcsv.php

https://bitbucket.org/matyhaty/senses-designertravelv3
PHP | 114 lines | 83 code | 14 blank | 17 comment | 10 complexity | f0f55ad28b39f9cdbe6cf359a5976f24 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. if (!defined('BASEPATH'))
  3. exit('No direct script access allowed');
  4. /**
  5. * TheBizzTech
  6. *
  7. * An open source library built for Codeigniter to read CSV files into associated arrays
  8. *
  9. * @author Jason Michels
  10. * @link http://thebizztech.com
  11. */
  12. class Getcsv
  13. {
  14. private $file_path = "";
  15. private $handle = "";
  16. public function set_file_path($file_path)
  17. {
  18. $this -> file_path = $file_path;
  19. return $this;
  20. }
  21. private function get_handle()
  22. {
  23. $this -> handle = fopen($this -> file_path, "r");
  24. return $this;
  25. }
  26. private function close_csv()
  27. {
  28. fclose($this -> handle);
  29. return $this;
  30. }
  31. //this is the most current function to use
  32. public function get_array()
  33. {
  34. $this -> get_handle();
  35. $row = 0;
  36. while (($data = fgetcsv($this -> handle, 0, ",")) !== FALSE)
  37. {
  38. if ($row == 0)
  39. {
  40. foreach ($data as $key => $value)
  41. {
  42. $title[$key] = trim($value);
  43. //this extracts the titles from the first row and builds array
  44. }
  45. }
  46. else
  47. {
  48. $new_row = $row - 1;
  49. //this is needed so that the returned array starts at 0 instead of 1
  50. foreach ($title as $key => $value)//this assumes there are as many columns as their are title columns
  51. {
  52. $result[$new_row][$value] = trim($data[$key]);
  53. }
  54. }
  55. $row++;
  56. }
  57. $this -> close_csv();
  58. return $result;
  59. }
  60. // --------------------------------Main Functions Above----------------------------------------------------- //
  61. //This function is being left in incase I ever need it
  62. function get_csv_array()
  63. {
  64. $row = 0;
  65. if (($handle = fopen($this -> file_path, "r")) !== FALSE)
  66. {
  67. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
  68. {
  69. $final_array[$row] = $data;
  70. $row++;
  71. }
  72. fclose($handle);
  73. }
  74. return $final_array;
  75. }
  76. //This function is being left in incase I ever need it
  77. function get_csv_array_headers($headers)
  78. {
  79. $row = 0;
  80. if (($handle = fopen($this -> file_path, "r")) !== FALSE)
  81. {
  82. while (($data = fgetcsv($handle, 10000, ",")) !== FALSE)
  83. {
  84. //print_r($data);
  85. foreach ($headers as $h_key => $h_value)
  86. {
  87. if ($h_value != 'useless')
  88. {
  89. $final_array[$row][$h_value] = $data[$h_key];
  90. }
  91. }
  92. //$final_array[$row] = $data;
  93. $row++;
  94. }
  95. fclose($handle);
  96. }
  97. return $final_array;
  98. }
  99. } //End of class
  100. //Here is the end of the getcsv.php class