diff options
author | cflip <36554078+cflip@users.noreply.github.com> | 2021-03-24 18:29:41 -0600 |
---|---|---|
committer | cflip <36554078+cflip@users.noreply.github.com> | 2021-03-24 18:29:41 -0600 |
commit | c9732788143886b611cb2fecfb53daf3d8add48f (patch) | |
tree | 8dd3152611bf59cc4982be9839e340ec89d0bee2 | |
parent | 9c619586639b9ac0f107e63a54b5522d75ea2ee2 (diff) |
Show latest thread/post on pages
-rw-r--r-- | category.php | 22 | ||||
-rw-r--r-- | index.php | 5 | ||||
-rw-r--r-- | model/Category.php | 22 | ||||
-rw-r--r-- | model/Thread.php | 22 |
4 files changed, 68 insertions, 3 deletions
diff --git a/category.php b/category.php index a71029e..ad4faa6 100644 --- a/category.php +++ b/category.php @@ -28,13 +28,31 @@ 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); + echo '<tr>'; echo '<td><b><a href="../thread/' . $thread->id . '">' . $thread->subject . '</a></b><br>'; - echo '<small>by ' . $thread->author->name . ' on ' . date('M d, Y', strtotime($thread->date_created)) . '</small></td>'; - echo '<td>' . date('M d, Y', strtotime($thread->date_lastpost)) . '</td>'; + echo '<small>by <b><a href="../user/' . $thread->author->name . '">' . $thread->author->name . '</a></b> on ' . date('M d, Y', strtotime($thread->date_created)) . '</small></td>'; + + if (!is_null($latest_post)) { + echo '<td>by <b><a href="../user/' . $latest_post->author->name . '">' . $latest_post->author->name . '</a></b><br><small>on ' . $latest_post->date . '</small></td>'; + } else { + echo '<td>No posts yet!</td>'; + } + echo '</tr>'; } ?> @@ -38,6 +38,8 @@ usort($categories, "cmp"); foreach ($categories as $category) { + $latest_thread = $category->get_latest_thread($dbc); + echo '<tr>'; echo '<td>'; echo '<b><a href="category/' . $category->id . '">' . $category->name . '</a></b>'; @@ -45,7 +47,8 @@ echo '</td>'; echo '<td>' . $category->thread_count . '</td>'; echo '<td>' . $category->post_count . '</td>'; - echo '<td><b>my supercool thread</b><br><small>by <b>cflip</b>, 3 days ago</small></td>'; + echo '<td><b><a href="./thread/' . $latest_thread->id . '">' . $latest_thread->subject . '</a></b><br>'; + echo '<small>by <b><a href="./user/' . $latest_thread->author->name . '">' . $latest_thread->author->name . '</a></b>, ' . $latest_thread->date_created . '</small></td>'; echo '</tr>'; } ?> diff --git a/model/Category.php b/model/Category.php index ffd903c..4e57f46 100644 --- a/model/Category.php +++ b/model/Category.php @@ -54,6 +54,28 @@ class Category { 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) { diff --git a/model/Thread.php b/model/Thread.php index ade24b5..20c6c0a 100644 --- a/model/Thread.php +++ b/model/Thread.php @@ -62,6 +62,28 @@ class Thread { mysqli_free_result($result); return $posts; } + + function get_latest_post($dbc) { + $sql = "SELECT post_id FROM posts WHERE post_thread = " . $this->id . " ORDER BY post_date DESC LIMIT 1"; + $result = mysqli_query($dbc, $sql); + + if (!$result) { + echo 'Could not get post from category: ' . mysqli_error($dbc); + } + + $post = null; + + if (mysqli_num_rows($result) == 0) { + } else { + while ($row = mysqli_fetch_assoc($result)) { + $post = new Post(); + $post->get_from_database($row['post_id'], $dbc); + } + } + + mysqli_free_result($result); + return $post; + } } function get_all_threads($dbc) { |