summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcflip <36554078+cflip@users.noreply.github.com>2021-03-24 19:01:51 -0600
committercflip <36554078+cflip@users.noreply.github.com>2021-03-24 19:01:51 -0600
commit0ecae4e72d7d4ace51b731ff2c5d5eb63351e3e1 (patch)
tree40ddad473bfc2e3e5c215da9ee0853044695737c
parentc9732788143886b611cb2fecfb53daf3d8add48f (diff)
Add thread replies, sort threads by last reply
-rw-r--r--create_thread.php31
-rw-r--r--includes/functions_insert.php35
-rw-r--r--index.php13
-rw-r--r--model/Category.php2
-rw-r--r--thread.php30
5 files changed, 68 insertions, 43 deletions
diff --git a/create_thread.php b/create_thread.php
index bc77cc7..dc0ce06 100644
--- a/create_thread.php
+++ b/create_thread.php
@@ -40,32 +40,7 @@ if (!isset($_SESSION['signed_in'])) {
</form>
<?php
include_once 'includes/db_inc.php';
-
-function insert_thread($dbc, $thread_subject, $thread_cat, $thread_author) {
- $sql = "INSERT INTO threads(thread_subject, thread_date_created, thread_date_lastpost, thread_category, thread_author) VALUES (?, CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), ?, ?);";
- $stmt = mysqli_stmt_init($dbc);
-
- if (!mysqli_stmt_prepare($stmt, $sql)) {
- die('Could not create thread due to internal error: ' . mysqli_error($dbc));
- }
-
- mysqli_stmt_bind_param($stmt, "sii", $thread_subject, $thread_cat, $thread_author);
- mysqli_stmt_execute($stmt);
- mysqli_stmt_close($stmt);
-}
-
-function insert_post($dbc, $post_content, $post_thread, $post_author) {
- $sql = "INSERT INTO posts(post_content, post_date, post_thread, post_author) VALUES (?, CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), ?, ?);";
- $stmt = mysqli_stmt_init($dbc);
-
- if (!mysqli_stmt_prepare($stmt, $sql)) {
- die('Could not create post due to internal error: ' . mysqli_error($dbc));
- }
-
- mysqli_stmt_bind_param($stmt, "sii", $post_content, $post_thread, $post_author);
- mysqli_stmt_execute($stmt);
- mysqli_stmt_close($stmt);
-}
+include_once 'includes/functions_insert.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$post_content = filter_input(INPUT_POST, 'post_content', FILTER_SANITIZE_STRING);
@@ -78,9 +53,9 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
} else {
insert_thread($dbc, $thread_subject, $thread_cat, $user_id);
$thread_id = mysqli_insert_id($dbc);
- insert_post($dbc, $post_content, $thread_id, $user_id);
+ insert_post($dbc, $post_content, $thread_id, $user_id, $thread_cat);
- $sql = "UPDATE categories SET `cat_thread_count` = `cat_thread_count` + '1', `cat_post_count` = `cat_post_count` + '1' WHERE cat_id = " . $thread_cat . ";";
+ $sql = "UPDATE categories SET `cat_thread_count` = `cat_thread_count` + '1' WHERE cat_id = " . $thread_cat . ";";
mysqli_query($dbc, $sql);
header("Location: /forum/thread/" . $thread_id);
diff --git a/includes/functions_insert.php b/includes/functions_insert.php
new file mode 100644
index 0000000..e13b80e
--- /dev/null
+++ b/includes/functions_insert.php
@@ -0,0 +1,35 @@
+<?php
+
+// This file may be replaced by a MVC controller later on
+
+function insert_thread($dbc, $thread_subject, $thread_cat, $thread_author) {
+ $sql = "INSERT INTO threads(thread_subject, thread_date_created, thread_date_lastpost, thread_category, thread_author) VALUES (?, CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), ?, ?);";
+ $stmt = mysqli_stmt_init($dbc);
+
+ if (!mysqli_stmt_prepare($stmt, $sql)) {
+ die('Could not create thread due to internal error: ' . mysqli_error($dbc));
+ }
+
+ mysqli_stmt_bind_param($stmt, "sii", $thread_subject, $thread_cat, $thread_author);
+ mysqli_stmt_execute($stmt);
+ mysqli_stmt_close($stmt);
+}
+
+function insert_post($dbc, $post_content, $post_thread, $post_author, $post_category) {
+ $sql = "INSERT INTO posts(post_content, post_date, post_thread, post_author) VALUES (?, CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), ?, ?);";
+ $stmt = mysqli_stmt_init($dbc);
+
+ if (!mysqli_stmt_prepare($stmt, $sql)) {
+ die('Could not create post due to internal error: ' . mysqli_error($dbc));
+ }
+
+ mysqli_stmt_bind_param($stmt, "sii", $post_content, $post_thread, $post_author);
+ mysqli_stmt_execute($stmt);
+ mysqli_stmt_close($stmt);
+
+ $sql = "UPDATE categories SET `cat_post_count` = `cat_post_count` + '1' WHERE cat_id = " . $post_category . ";";
+ mysqli_query($dbc, $sql);
+
+ $sql = "UPDATE threads SET thread_date_lastpost = CONVERT_TZ(NOW(), 'SYSTEM', '+00:00') WHERE thread_id = " . $post_thread . ";";
+ mysqli_query($dbc, $sql);
+} \ No newline at end of file
diff --git a/index.php b/index.php
index b4b00a1..dcad956 100644
--- a/index.php
+++ b/index.php
@@ -53,18 +53,5 @@
}
?>
</table>
- <h2>More from the forum</h2>
- <table>
- <tr>
- <th>Recent Posts</th>
- <th>Recent Threads</th>
- <th>Popular Threads</th>
- </tr>
- <tr>
- <td>test<br>test<br>test<br>test<br></td>
- <td>test<br>test<br>test<br>test<br></td>
- <td>test<br>test<br>test<br>test<br></td>
- </tr>
- </table>
</body>
</html> \ No newline at end of file
diff --git a/model/Category.php b/model/Category.php
index 4e57f46..5a2c11c 100644
--- a/model/Category.php
+++ b/model/Category.php
@@ -33,7 +33,7 @@ class Category {
}
function get_threads($dbc) {
- $sql = "SELECT thread_id FROM threads WHERE thread_category = " . $this->id;
+ $sql = "SELECT thread_id FROM threads WHERE thread_category = " . $this->id . " ORDER BY thread_date_lastpost";
$result = mysqli_query($dbc, $sql);
if (!$result) {
diff --git a/thread.php b/thread.php
index 3a65114..0d516da 100644
--- a/thread.php
+++ b/thread.php
@@ -30,5 +30,33 @@ if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT)) {
$post->display_content();
}
?>
+ <hr>
+ <h2>Reply to this thread</h2>
+ <form method="post">
+ <textarea name="post_content" rows="10" cols="50"></textarea>
+ <br>
+ <input type="submit" name="submit">
+ </form>
</body>
-</html> \ No newline at end of file
+</html>
+<?php
+include_once 'includes/db_inc.php';
+include_once 'includes/functions_insert.php';
+
+if ($_SERVER['REQUEST_METHOD'] == 'POST') {
+ if (!isset($_SESSION['signed_in'])) {
+ echo 'You must be <a href="signin.php">signed in</a> to reply to this thread.';
+ return;
+ }
+
+ $post_content = filter_input(INPUT_POST, 'post_content', FILTER_SANITIZE_STRING);
+ $user_id = filter_var($_SESSION['user_id'], FILTER_SANITIZE_NUMBER_INT);
+
+ if (empty($post_content) or !$post_content) {
+ echo 'Thread subject cannot be empty';
+ } else {
+ insert_post($dbc, $post_content, $current->id, $user_id, $current->category->id);
+ }
+}
+
+?> \ No newline at end of file