b4c55775a0ff8807e0888f4a535ec45d6ad9966a
[dylansserver.git] / page.php
1 <?php
2
3 class page extends model {
4
5 public $page = 1;
6 public $offset = 0;
7 public $notes_per_page = 4;
8 public $number_of_pages = 1;
9 public $notes = array();
10
11 public function __construct() {
12 parent::__construct();
13 $this->page_offset();
14 $this->fetch_notes();
15 }
16
17 private function page_offset() {
18 $sql = "SELECT COUNT(*) FROM notes";
19 $result = $this->db->query($sql);
20 $result = $result->fetch_array();
21 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
22 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
23 $this->page = (int) $_GET['page'];
24 } else {
25 throw new notFound();
26 }
27 if ($this->page > $this->number_of_pages) {
28 throw new notFound();
29 }
30 if ($this->page < 1) {
31 throw new notFound();
32 }
33 $this->offset = ($this->page - 1) * $this->notes_per_page;
34 }
35
36 public function display() {
37 require_once("view/page.php");
38 }
39
40 protected function fetch_notes() {
41 $sql = "SELECT date_posted, title, url, text
42 FROM notes ORDER BY date_posted DESC
43 LIMIT ?, ?";
44 $result = $this->query($sql, "ii",
45 $this->offset,
46 $this->notes_per_page);
47 foreach ($result as $row => $entry) {
48 $entry['url'] = '/note/' . $entry['url'];
49 $date_posted = explode("-", $entry['date_posted']);
50 $entry['year_posted'] = $date_posted[0];
51 $entry['month_posted'] = $date_posted[1];
52 $entry['datetime_posted'] = explode(' ', $date_posted[2]);
53 $entry['day_posted'] = $entry['date_posted'][0];
54 $this->notes[$row] = $entry;
55 }
56 }
57 }
58
59 ?>