diff options
author | Cflip <36554078+cflip@users.noreply.github.com> | 2021-01-24 14:14:05 -0700 |
---|---|---|
committer | Cflip <36554078+cflip@users.noreply.github.com> | 2021-01-24 14:14:05 -0700 |
commit | 147eadd2c2bb6c8c2c9e238a82296c8a840c4aa8 (patch) | |
tree | d9588a6e141384d8329ea0951badbb8edb31bb0e | |
parent | 6500ec0543a316f9fa5f16a5c47e7103ec9bbe9e (diff) |
Create function to display threads
-rw-r--r-- | category.php | 28 | ||||
-rw-r--r-- | includes/functions_inc.php | 30 | ||||
-rw-r--r-- | index.php | 10 |
3 files changed, 36 insertions, 32 deletions
diff --git a/category.php b/category.php index b26856a..9fffd10 100644 --- a/category.php +++ b/category.php @@ -2,6 +2,7 @@ include_once 'includes/db_inc.php'; include_once 'header.php'; +include_once 'includes/functions_inc.php'; echo '<section>'; @@ -32,34 +33,11 @@ $result = mysqli_query($dbc, $sql); if (!$result) { die('Error trying to display threads: ' . mysqli_error($dbc)); -} - -echo '<table><tr><th class="left">Thread</th><th class="right">Latest Post</th></tr>'; - -$sql = "SELECT post_id, post_date, user_id, user_name FROM posts JOIN users ON post_author = user_id WHERE post_thread = ? ORDER BY post_id DESC LIMIT 1"; -$stmt = mysqli_stmt_init($dbc); - -if (!mysqli_stmt_prepare($stmt, $sql)) { - die('Could not create thread due to internal error: ' . mysqli_error($dbc)); -} - -while ($row = mysqli_fetch_assoc($result)) { - mysqli_stmt_bind_param($stmt, "i", $row['thread_id']); - mysqli_stmt_execute($stmt); - - $thread_res = mysqli_stmt_get_result($stmt); - $thread = mysqli_fetch_assoc($thread_res); - - echo '<tr><td class="left">'; - echo '<h4><a href="thread.php?id=' . $row['thread_id'] . '">' . $row['thread_subject'] . '</a></h4>'; - echo '<small>by <b><a href="user.php?id=' . $row['user_id'] . '">' . $row['user_name'] . '</a></b> on ' . date('M d, Y', strtotime($row['thread_date'])) . '</small>'; - echo '</td><td class="right">by <b><a href="user.php?id=' . $row['user_id'] . '">' . $thread['user_name'] . '</a></b><br>'; - echo '<small>' . date('m/d/Y g:ia', strtotime($thread['post_date'])) . '</small></td></tr>'; } -mysqli_stmt_close($stmt); +echo '<table><tr><th class="left">Thread</th><th class="right">Latest Post</th></tr>'; +display_threads($dbc, $result); mysqli_free_result($result); - echo '</table>'; include 'footer.php';
\ No newline at end of file diff --git a/includes/functions_inc.php b/includes/functions_inc.php new file mode 100644 index 0000000..c10b65b --- /dev/null +++ b/includes/functions_inc.php @@ -0,0 +1,30 @@ +<?php + +function display_threads($dbc, $sql_result, $show_category = false) { + $sql = "SELECT post_id, post_date, user_id, user_name FROM posts JOIN users ON post_author = user_id WHERE post_thread = ? ORDER BY post_id DESC LIMIT 1"; + $stmt = mysqli_stmt_init($dbc); + + if (!mysqli_stmt_prepare($stmt, $sql)) { + die('Could not create thread due to internal error: ' . mysqli_error($dbc)); + } + + while ($row = mysqli_fetch_assoc($sql_result)) { + mysqli_stmt_bind_param($stmt, "i", $row['thread_id']); + mysqli_stmt_execute($stmt); + + $thread_res = mysqli_stmt_get_result($stmt); + $thread = mysqli_fetch_assoc($thread_res); + + echo '<tr><td class="left">'; + echo '<h4><a href="thread.php?id=' . $row['thread_id'] . '">' . $row['thread_subject'] . '</a></h4>'; + echo '<small>by <b><a href="user.php?id=' . $row['user_id'] . '">' . $row['user_name'] . '</a></b> '; + if ($show_category) { + echo 'in <b><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></b> '; + } + echo 'on ' . date('M d, Y', strtotime($row['thread_date'])) . '</small>'; + echo '</td><td class="right">by <b><a href="user.php?id=' . $thread['user_id'] . '">' . $thread['user_name'] . '</a></b><br>'; + echo '<small>' . date('m/d/Y g:ia', strtotime($thread['post_date'])) . '</small></td></tr>'; + } + + mysqli_stmt_close($stmt); +}
\ No newline at end of file @@ -57,24 +57,20 @@ include_once 'header.php'; <table> <tr> <th class="left">Latest Threads</th> - <th class="right">Categories</th> + <th class="right">Latest Post</th> </tr> <?php include_once 'includes/db_inc.php'; + include_once 'includes/functions_inc.php'; $sql = "SELECT thread_id, thread_subject, thread_date, user_id, user_name, cat_id, cat_name FROM threads JOIN users ON thread_author = user_id JOIN categories ON thread_cat = cat_id ORDER BY thread_id DESC LIMIT 5"; $result = mysqli_query($dbc, $sql); if (!$result) { die('Error trying to display threads: ' . mysqli_error($dbc)); - } - - while ($row = mysqli_fetch_assoc($result)) { - echo '<tr><td class="left">'; - echo '<h4><a href="thread.php?id=' . $row['thread_id'] . '">' . $row['thread_subject'] . '</a></h4>'; - echo '<small>by <b><a href="user.php?id=' . $thread['user_id'] . '">' . $row['user_name'] . '</a></b> on ' . date('M d, Y', strtotime($row['thread_date'])) . '</small></td><td class="right">Posted in <a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] .'</a></td></tr>'; } + display_threads($dbc, $result, true); mysqli_free_result($result); ?> </table> |