all page types now served with model/view
[dylansserver.git] / model / 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
10 public function __construct() {
11 parent::__construct();
12 $this->page_offset();
13 }
14
15 private function page_offset() {
16 $sql = "SELECT COUNT(*) FROM notes";
17 $result = $this->db->query($sql);
18 $result = $result->fetch_array();
19 $this->number_of_pages = ceil($result[0] / $this->notes_per_page);
20 if (isset($_GET['page']) && is_numeric($_GET['page'])) {
21 $this->page = (int) $_GET['page'];
22 } else {
23 throw new notFound();
24 }
25 if ($this->page > $this->number_of_pages) {
26 throw new notFound();
27 }
28 if ($this->page < 1) {
29 throw new notFound();
30 }
31 $this->offset = ($this->page - 1) * $this->notes_per_page;
32 }
33
34 public function display() {
35 require_once("view/page.php");
36 }
37
38 public function display_notes() {
39 echo "<div id='notes'>";
40 $sql = "SELECT date_posted, title, url, text
41 FROM notes ORDER BY date_posted DESC
42 LIMIT ?, ?";
43 $result = $this->query($sql, "ii",
44 $this->offset,
45 $this->notes_per_page);
46 foreach ($result as $row => $entry) {
47 $title = $entry['title'];
48 $url = '/note/' . $entry['url'];
49 $date_posted = explode("-", $entry['date_posted']);
50 $year_posted = $date_posted[0];
51 $month_posted = $date_posted[1];
52 $datetime_posted = explode(' ', $date_posted[2]);
53 $day_posted = $datetime_posted[0];
54 $text = $entry['text'];
55 echo <<<END_NOTE
56 <div class='note'>
57 <h1>
58 <span class='date'>$year_posted/$month_posted/$day_posted/</span><a rel="canonical" href='$url'>$title</a>
59 </h1>
60 $text
61 </div>
62 END_NOTE;
63 }
64 echo "</div>";
65 }
66 }
67
68 ?>