Exemplo n.º 1
0
 public function getIndex()
 {
     $sql = "SELECT p.*, u.id as author_id, u.username FROM posts as p ";
     $sql .= "INNER JOIN users as u ON u.id = p.user_id ORDER BY created_at DESC";
     $db = Db::getInstance();
     $posts = $db->connect->query($sql)->fetchAll(\PDO::FETCH_OBJ);
     $count = $db->connect->query("SELECT COUNT(*) FROM posts")->fetchColumn();
     // echo '<pre>';
     // var_dump($posts);
     // echo '</pre>';
     // return ;
     return $this->render('page/index', compact('posts', 'count'));
 }
Exemplo n.º 2
0
 private function findPost()
 {
     $db = Db::getInstance();
     $id = $_GET['id'];
     $sql = "SELECT id, title, body, created_at FROM posts where id = :id";
     $stmt = $db->connect->prepare($sql);
     $stmt->bindParam(':id', $id);
     $stmt->execute();
     $post = $stmt->fetchAll(\PDO::FETCH_OBJ)[0];
     return $post;
 }