summaryrefslogtreecommitdiff
path: root/includes/model/Category.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/model/Category.php')
-rw-r--r--includes/model/Category.php103
1 files changed, 103 insertions, 0 deletions
diff --git a/includes/model/Category.php b/includes/model/Category.php
new file mode 100644
index 0000000..b7c46d9
--- /dev/null
+++ b/includes/model/Category.php
@@ -0,0 +1,103 @@
+<?php
+
+include_once 'Thread.php';
+
+class Category {
+ public $id = 0;
+ public $name = 'Unknown';
+ public $description = 'This category does not exist';
+ public $thread_count = 0;
+ public $post_count = 0;
+
+ function get_from_database($id, $dbc) {
+ $sql = "SELECT cat_name, cat_description, cat_thread_count, cat_post_count FROM categories WHERE cat_id = " . mysqli_real_escape_string($dbc, $id);
+ $result = mysqli_query($dbc, $sql);
+
+ if (!$result) {
+ echo 'Failed to get category: ' . mysqli_error($dbc);
+ }
+
+ if (mysqli_num_rows($result) == 0) {
+ return 0;
+ } else {
+ while ($row = mysqli_fetch_assoc($result)) {
+ $this->id = $id;
+ $this->name = $row['cat_name'];
+ $this->description = $row['cat_description'];
+ $this->thread_count = $row['cat_thread_count'];
+ $this->post_count = $row['cat_post_count'];
+ }
+ }
+
+ mysqli_free_result($result);
+ return 1;
+ }
+
+ function get_threads($dbc) {
+ $sql = "SELECT thread_id FROM threads WHERE thread_category = " . $this->id . " ORDER BY thread_date_lastpost DESC";
+ $result = mysqli_query($dbc, $sql);
+
+ if (!$result) {
+ echo 'Could not get threads from category: ' . mysqli_error($dbc);
+ }
+
+ $threads = array();
+
+ if (mysqli_num_rows($result) == 0) {
+ } else {
+ while ($row = mysqli_fetch_assoc($result)) {
+ $thread = new Thread();
+ $thread->get_from_database($row['thread_id'], $dbc);
+ array_push($threads, $thread);
+ }
+ }
+
+ mysqli_free_result($result);
+ return $threads;
+ }
+
+ function get_latest_thread($dbc) {
+ $sql = "SELECT thread_id FROM threads WHERE thread_category = " . $this->id . " ORDER BY thread_date_lastpost DESC LIMIT 1";
+ $result = mysqli_query($dbc, $sql);
+
+ if (!$result) {
+ echo 'Could not get thread from category: ' . mysqli_error($dbc);
+ }
+
+ $thread = null;
+
+ if (mysqli_num_rows($result) == 0) {
+ } else {
+ while ($row = mysqli_fetch_assoc($result)) {
+ $thread = new Thread();
+ $thread->get_from_database($row['thread_id'], $dbc);
+ }
+ }
+
+ mysqli_free_result($result);
+ return $thread;
+ }
+}
+
+function get_all_categories($dbc) {
+ $sql = "SELECT cat_id FROM categories ORDER BY cat_id ASC;";
+ $result = mysqli_query($dbc, $sql);
+
+ if (!$result) {
+ echo 'Failed to get categories: ' . mysqli_error($dbc);
+ }
+
+ $categories = array();
+
+ if (mysqli_num_rows($result) == 0) {
+ } else {
+ while ($row = mysqli_fetch_assoc($result)) {
+ $category = new Category();
+ $category->get_from_database($row['cat_id'], $dbc);
+ array_push($categories, $category);
+ }
+ }
+
+ mysqli_free_result($result);
+ return $categories;
+} \ No newline at end of file