Category.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. require_once("DataBase/database.php");
  3. class Category
  4. {
  5. function __construct()
  6. {
  7. $this->db = new Database();
  8. }
  9. function create($value)
  10. {
  11. $query = "INSERT INTO ".$this->db->category_table." VALUES('".$value[0]."'";
  12. for($i = 1; $i < count($value); $i++)
  13. $query .= ", '".str_replace("'", "\'", $value[$i])."'";
  14. $query .= ")";
  15. $this->db->query($query);
  16. }
  17. function update($category_id, $value)
  18. {
  19. $query = "UPDATE ".$this->db->category_table." SET ";
  20. $query .= "name='".$value[0]."', ";
  21. $query .= "details='".$value[1]."' ";
  22. $query .= "WHERE category_id='".$category_id."'";
  23. $this->db->query($query);
  24. }
  25. function delete($category_id)
  26. {
  27. $query = "DELETE FROM ".$this->db->category_table." WHERE category_id='$category_id'";
  28. $this->db->query($query);
  29. }
  30. function get($category_id = "", $row_names = array(), $search = "")
  31. {
  32. if($category_id == "")
  33. {
  34. if(count($row_names) == 0)
  35. $query = "SELECT * FROM ".$this->db->category_table;
  36. else
  37. {
  38. $query = "SELECT ".$row_names[0];
  39. for($i = 1; $i < count($row_names); $i++)
  40. $query .= ", ".$row_names[$i];
  41. $query .= " FROM ".$this->db->category_table;
  42. }
  43. if($search != "")
  44. $query .= " WHERE ".$search;
  45. $query .= " ORDER BY category_id ASC";
  46. return $this->db->fetchRows($this->db->query($query));
  47. }
  48. else
  49. {
  50. $query = "SELECT * FROM ".$this->db->category_table." WHERE category_id='".$category_id."' ORDER BY category_id ASC";
  51. return $this->db->fetchRows($this->db->query($query));
  52. }
  53. }
  54. function getABC($category_id = "", $row_names = array(), $search = "")
  55. {
  56. if($category_id == "")
  57. {
  58. if(count($row_names) == 0)
  59. $query = "SELECT * FROM ".$this->db->category_table;
  60. else
  61. {
  62. $query = "SELECT ".$row_names[0];
  63. for($i = 1; $i < count($row_names); $i++)
  64. $query .= ", ".$row_names[$i];
  65. $query .= " FROM ".$this->db->category_table;
  66. }
  67. if($search != "")
  68. $query .= " WHERE ".$search;
  69. $query .= " ORDER BY name ASC";
  70. return $this->db->fetchRows($this->db->query($query));
  71. }
  72. else
  73. {
  74. $query = "SELECT * FROM ".$this->db->category_table." WHERE category_id='".$category_id."' ORDER BY category_id ASC";
  75. return $this->db->fetchRows($this->db->query($query));
  76. }
  77. }
  78. function clearTable()
  79. {
  80. $query = "TRUNCATE TABLE ".$this->db->category_table;
  81. $this->db->query($query);
  82. }
  83. function export($path)
  84. {
  85. $csv_data = "";
  86. $query = "SELECT * FROM ".$this->db->category_table;
  87. $result = $this->db->query($query);
  88. while ($row = mysqli_fetch_assoc($result))
  89. $csv_data .= implode('|', $row)."\n";
  90. $this->db->writeToFile($path, $csv_data);
  91. }
  92. function import($path)
  93. {
  94. $file_access_ok = true;
  95. $file_open = false;
  96. $file_handle = false;
  97. $data = "";
  98. clearstatcache();
  99. if (!$file_handle = fopen($path, "r"))
  100. {
  101. echo "could not open file";
  102. $file_access_ok = false;
  103. }
  104. if($file_handle)
  105. {
  106. $this->clearTable();
  107. while( $zeile = fgets($file_handle, 1024))
  108. {
  109. $values = explode("|", $zeile);
  110. for($i = 0; $i < count($values); $i++)
  111. $values[$i] = trim($values[$i]);
  112. $this->create(array( $values[0],
  113. $values[1],
  114. $values[2]));
  115. }
  116. fclose($file_handle);
  117. }
  118. }
  119. }
  120. ?>