Site du proximo, utilisé pour gérer le stock.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dao.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. class Dao
  3. {
  4. private $conn;
  5. public function __construct()
  6. {
  7. $username = 'proximo';
  8. $password = $this->read_password();
  9. $dsn = 'mysql:dbname=proximo;host=127.0.0.1';
  10. try {
  11. $this->conn = new PDO($dsn, $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']);
  12. } catch (PDOException $e) {
  13. echo "error";
  14. echo $e;
  15. }
  16. }
  17. private function read_password()
  18. {
  19. $real_path = __DIR__ . DIRECTORY_SEPARATOR . ".htpassdb";
  20. $file = fopen($real_path, "r") or die("Unable to open DB password file!");
  21. $password = fgets($file);
  22. fclose($file);
  23. return trim($password);
  24. }
  25. public function get_articles()
  26. {
  27. $sql = 'SELECT * FROM articles';
  28. $cursor = $this->conn->prepare($sql);
  29. $cursor->execute();
  30. return $cursor->fetchAll(PDO::FETCH_ASSOC);
  31. }
  32. public function get_article_categories($article_id)
  33. {
  34. $sql = 'SELECT category_id FROM article_categories WHERE article_id=?';
  35. $cursor = $this->conn->prepare($sql);
  36. $cursor->execute([$article_id]);
  37. $result = $cursor->fetchAll(PDO::FETCH_ASSOC);
  38. $final = [];
  39. foreach ($result as $row) {
  40. array_push($final, $row["category_id"]);
  41. }
  42. return $final;
  43. }
  44. public function get_categories()
  45. {
  46. $sql = 'SELECT * FROM categories';
  47. $cursor = $this->conn->prepare($sql);
  48. $cursor->execute();
  49. return $cursor->fetchAll(PDO::FETCH_ASSOC);
  50. }
  51. public function create_category($category) {
  52. $sql = 'INSERT INTO categories (name, icon) VALUES (?, ?)';
  53. $cursor = $this->conn->prepare($sql);
  54. $data = [$category["name"], $category["icon"]];
  55. $cursor->execute($data);
  56. return $this->conn->lastInsertId();
  57. }
  58. public function update_category($category)
  59. {
  60. $sql = 'UPDATE categories SET name=?, icon=? WHERE id=?';
  61. $cursor = $this->conn->prepare($sql);
  62. $data = [$category["name"], $category["icon"], $category["id"]];
  63. $cursor->execute($data);
  64. return $category["id"];
  65. }
  66. public function remove_category($id)
  67. {
  68. $sql = 'DELETE FROM categories WHERE id=?';
  69. $cursor = $this->conn->prepare($sql);
  70. $data = [$id];
  71. $result = $cursor->execute($data);
  72. if ($result)
  73. return $cursor->rowCount();
  74. else
  75. return 0;
  76. }
  77. }