-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathIndex.php
42 lines (32 loc) · 1.44 KB
/
Index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
class Index {
protected $db;
public function __construct($config) {
$dbconfig = $config['database'];
$dsn = 'mysql:host=' . $dbconfig['host'] . ';dbname=' . $dbconfig['name'];
$this->db = new PDO($dsn, $dbconfig['user'], $dbconfig['pass']);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function index() {
$sql = 'SELECT * FROM story ORDER BY created_on DESC';
$stmt = $this->db->prepare($sql);
$stmt->execute();
$stories = $stmt->fetchAll(PDO::FETCH_ASSOC);
$content = '<ol>';
foreach($stories as $story) {
$comment_sql = 'SELECT COUNT(*) as `count` FROM comment WHERE story_id = ?';
$comment_stmt = $this->db->prepare($comment_sql);
$comment_stmt->execute(array($story['id']));
$count = $comment_stmt->fetch(PDO::FETCH_ASSOC);
$content .= '
<li>
<a class="headline" href="' . $story['url'] . '">' . $story['headline'] . '</a><br />
<span class="details">' . $story['created_by'] . ' | <a href="/story/?id=' . $story['id'] . '">' . $count['count'] . ' Comments</a> |
' . date('n/j/Y g:i a', strtotime($story['created_on'])) . '</span>
</li>
';
}
$content .= '</ol>';
require 'layout.phtml';
}
}