public function get() { $bdd = new Database('home'); $get = $bdd->getBdd()->prepare('SELECT id, name FROM categories WHERE active = 1'); $get->execute(); $all = $get->fetchAll(\PDO::FETCG_ASSOC); return json_encode($all); }
public function getByPost($id) { $bdd = new Database('home'); $get = $bdd->getBdd()->prepare('SELECT * FROM media WHERE post_id = :post_id'); $get->bindParam(':post_id', $id); $get->execute(); $medias = $get->fetchAll(\PDO::FETCH_ASSOC); return $medias; }
public function setMinusComment($comment_id) { $bdd = new Database('home'); $get_comment = $bdd->getBdd()->prepare('SELECT * FROM comments WHERE id = :id'); $get_comment->bindParam(':id', $comment_id, \PDO::PARAM_INT); $get_comment->execute(); $comment = $get_comment->fetch(\PDO::FETCH_ASSOC); if ($comment === false) { $comment = array('error' => 'comment id invalid'); return json_encode($comment); } else { $current_comment_vote = $comment['vote']; $new_vote = $current_comment_vote - 1; $vote_minus = $bdd->getBdd()->prepare('UPDATE comments SET vote = ' . $new_vote . ' WHERE id = :id'); $vote_minus->bindParam(':id', $comment_id, \PDO::PARAM_INT); $vote_minus->execute(); return true; } }
public function create($blog_id, $title, $content) { $bdd = new Database('home'); $create = $bdd->getBdd()->prepare('INSERT INTO mails (blog_id, title, content, user_id) VALUES (:blog_id, :title, :content, :user_id)'); $create->bindParam(':blog_id', $blog_id); $create->bindParam(':title', $title); $create->bindParam(':content', $content); $create->bindParam(':user_id', $_SESSION['id']); $create->execute(); return true; }
public function readPost($id, $json = 0) { $bdd = new Database('home'); $read = $bdd->getBdd()->prepare('SELECT * FROM posts WHERE id = :id AND active = 1'); $read->bindParam(':id', $id); $read->execute(); $post = $read->fetch(\PDO::FETCH_ASSOC); if ($json == 0) { $this->setPost($post); return true; } else { if (empty($post)) { $post = array('error' => 'post id invalid'); } else { $medias = new MediasController(); $nb_comments = $bdd->getBdd()->prepare('SELECT COUNT(id) AS nb_comments FROM comments WHERE post_id = :post_id'); $nb_comments->bindParam(':post_id', $post['id'], \PDO::PARAM_INT); $nb_comments->execute(); $nb_comments = $nb_comments->fetch(\PDO::FETCH_ASSOC); $post['comments'] = array(); if ($nb_comments["nb_comments"] !== 0) { $all_post_comments = $bdd->getBdd()->prepare('SELECT users.id AS "user_id", comments.id AS "comment_id", users.name AS "user_name", title, content, score, vote FROM comments LEFT JOIN users ON users.id = comments.user_id WHERE post_id = :post_id'); $all_post_comments->bindParam(':post_id', $post['id'], \PDO::PARAM_INT); $all_post_comments->execute(); $all_post_comments = $all_post_comments->fetchAll(); foreach ($all_post_comments as $comment) { $post['comments']['comment_id'][] = $comment['comment_id']; $post['comments']['user_id'][] = $comment['user_id']; $post['comments']['user_name'][] = $comment['user_name']; $post['comments']['title'][] = $comment['title']; $post['comments']['content'][] = $comment['content']; $post['comments']['score'][] = $comment['score']; $post['comments']['vote'][] = $comment['vote']; } } $post['nb_comments'] = $nb_comments['nb_comments']; $post['medias'] = $medias->getByPost($post['id']); } return json_encode($post); } }
private function _updateCheckEmail($email) { $bdd = new Database('home'); $id = isset($_SESSION['id']) ? $_SESSION['id'] : 0; $check = $bdd->getBdd()->prepare('SELECT email FROM users WHERE email = :email AND id != :id AND active = 1'); $check->bindParam(':email', $email, \PDO::PARAM_STR, 60); $check->bindParam(':id', $id); $check->execute(); $user = $check->fetch(\PDO::FETCH_ASSOC); if ($user) { return false; } return true; }
public function getMyBlogs() { $bdd = new Database('home'); $get = $bdd->getBdd()->prepare('SELECT id, name FROM blogs WHERE user_id = :user_id AND active = 1'); $get->bindParam(':user_id', $_SESSION['id']); $get->execute(); $all = $get->fetchAll(\PDO::FETCH_ASSOC); if (empty($all)) { $this->setError('You have no blog'); } $this->_blogs = $all; }