summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcflip <36554078+cflip@users.noreply.github.com>2021-04-17 17:36:18 -0600
committercflip <36554078+cflip@users.noreply.github.com>2021-04-17 17:36:18 -0600
commitfaae3f3906575b9c72d628a5e48d03f943c794fc (patch)
treeae3290ba4a1377f95f5c2081ffebe7124958872c
parent0b045d57b2164b5ce003955d79627ae506a153eb (diff)
Automatically sort threads without PHP comparisons
-rw-r--r--index.php9
-rw-r--r--model/Category.php4
-rw-r--r--viewcategory.php10
3 files changed, 2 insertions, 21 deletions
diff --git a/index.php b/index.php
index 7dd0fe4..bdbf770 100644
--- a/index.php
+++ b/index.php
@@ -27,16 +27,7 @@
include_once 'includes/db_inc.php';
include_once 'model/Category.php';
- // TODO: The get_all_categories function should return them in the right order
- function cmp($a, $b) {
- if ($a->id == $b->id) {
- return 0;
- }
- return ($a->id < $b->id) ? -1 : 1;
- }
-
$categories = get_all_categories($dbc);
- usort($categories, "cmp");
foreach ($categories as $category) {
$latest_thread = $category->get_latest_thread($dbc);
diff --git a/model/Category.php b/model/Category.php
index 1b699fb..b7c46d9 100644
--- a/model/Category.php
+++ b/model/Category.php
@@ -34,7 +34,7 @@ class Category {
}
function get_threads($dbc) {
- $sql = "SELECT thread_id FROM threads WHERE thread_category = " . $this->id . " ORDER BY thread_date_lastpost";
+ $sql = "SELECT thread_id FROM threads WHERE thread_category = " . $this->id . " ORDER BY thread_date_lastpost DESC";
$result = mysqli_query($dbc, $sql);
if (!$result) {
@@ -80,7 +80,7 @@ class Category {
}
function get_all_categories($dbc) {
- $sql = "SELECT cat_id FROM categories";
+ $sql = "SELECT cat_id FROM categories ORDER BY cat_id ASC;";
$result = mysqli_query($dbc, $sql);
if (!$result) {
diff --git a/viewcategory.php b/viewcategory.php
index a10afce..e10797a 100644
--- a/viewcategory.php
+++ b/viewcategory.php
@@ -39,17 +39,7 @@ if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT)) {
<th>Latest Post</th>
</tr>
<?php
- function cmp($a, $b) {
- $da = strtotime($a->date_lastpost);
- $db = strtotime($b->date_lastpost);
-
- if ($da == $db) return 0;
-
- return ($da > $db) ? -1 : 1;
- }
-
$threads = $current->get_threads($dbc);
- usort($threads, "cmp");
foreach ($threads as $thread) {
$latest_post = $thread->get_latest_post($dbc);