123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
-
-
- class Dao
- {
- private $conn;
-
- public function __construct()
- {
- $username = 'proximo';
- $password = $this->read_password();
- $dsn = 'mysql:dbname=proximo;host=127.0.0.1';
- try {
- $this->conn = new PDO($dsn, $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']);
- } catch (PDOException $e) {
- echo "error";
- echo $e;
- }
- }
-
- private function read_password()
- {
- $real_path = __DIR__ . DIRECTORY_SEPARATOR . ".htpassdb";
- $file = fopen($real_path, "r") or die("Unable to open DB password file!");
- $password = fgets($file);
- fclose($file);
- return trim($password);
- }
-
- public function get_articles()
- {
- $sql = 'SELECT * FROM articles';
- $cursor = $this->conn->prepare($sql);
- $cursor->execute();
- return $cursor->fetchAll(PDO::FETCH_ASSOC);
- }
-
- public function get_article_categories($article_id)
- {
- $sql = 'SELECT category_id FROM article_categories WHERE article_id=?';
- $cursor = $this->conn->prepare($sql);
- $cursor->execute([$article_id]);
- $result = $cursor->fetchAll(PDO::FETCH_ASSOC);
- $final = [];
- foreach ($result as $row) {
- array_push($final, $row["category_id"]);
- }
- return $final;
- }
-
- public function get_categories()
- {
- $sql = 'SELECT * FROM categories';
- $cursor = $this->conn->prepare($sql);
- $cursor->execute();
- return $cursor->fetchAll(PDO::FETCH_ASSOC);
- }
-
- public function create_category($category) {
- $sql = 'INSERT INTO categories (name, icon) VALUES (?, ?)';
- $cursor = $this->conn->prepare($sql);
- $data = [$category["name"], $category["icon"]];
- $cursor->execute($data);
- return $this->conn->lastInsertId();
- }
-
- public function update_category($category)
- {
- $sql = 'UPDATE categories SET name=?, icon=? WHERE id=?';
- $cursor = $this->conn->prepare($sql);
- $data = [$category["name"], $category["icon"], $category["id"]];
- $cursor->execute($data);
- return $category["id"];
- }
-
- public function remove_category($id)
- {
- $sql = 'DELETE FROM categories WHERE id=?';
- $cursor = $this->conn->prepare($sql);
- $data = [$id];
- $result = $cursor->execute($data);
- if ($result)
- return $cursor->rowCount();
- else
- return 0;
- }
- }
|