diff options
author | Cflip <36554078+cflip@users.noreply.github.com> | 2021-02-10 20:40:32 -0700 |
---|---|---|
committer | Cflip <36554078+cflip@users.noreply.github.com> | 2021-02-10 20:40:32 -0700 |
commit | f83530a122119d7f69812493f9c2f4987ccb2065 (patch) | |
tree | 691ed8597a8d3275998f7db951b7b055ef5baf3b /includes/functions_display.php | |
parent | 4c9d433ba1c52ad67e4cccabf04e709bb8b85070 (diff) |
Reorganize code and add info to front page
Diffstat (limited to 'includes/functions_display.php')
-rw-r--r-- | includes/functions_display.php | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/includes/functions_display.php b/includes/functions_display.php new file mode 100644 index 0000000..479648f --- /dev/null +++ b/includes/functions_display.php @@ -0,0 +1,118 @@ +<?php + +function display_navbar($dbc) { + +} + +function display_categories($dbc, $sql_result) { + $sql = "SELECT thread_id, thread_subject, thread_date, user_id, user_name FROM threads JOIN users ON thread_author = user_id WHERE thread_cat = ? ORDER BY thread_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['cat_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="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h4>'; + echo $row['cat_description']; + if ($thread) { + echo '</td><td class="right">' . $thread['thread_subject'] . '<br>'; + echo '<small>by <b><a href="user.php?id=' . $thread['user_id'] . '">' . $thread['user_name'] . '</a></b></small></td></tr>'; + } else { + $no_threads_msg = 'There are no threads in this category yet.'; + echo '</td><td class="right"><small>'. $no_threads_msg .'</small></td>'; + } + } + + mysqli_stmt_close($stmt); + mysqli_free_result($thread_res); +} + +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); +} + +function add_quote($dbc, $thread_id, $matches) { + foreach ($matches as $match) { + $id = (int) filter_var($match, FILTER_SANITIZE_NUMBER_INT) - 1; + $sql = "SELECT post_content, post_author, user_name FROM posts LEFT JOIN users ON post_author = user_id WHERE post_thread = " . $thread_id . " LIMIT 1 OFFSET " . $id; + $result = mysqli_query($dbc, $sql); + + if (!$result) { + return '<blockquote></blockquote>'; + } + + $reply = mysqli_fetch_assoc($result); + + if (empty($reply)) { + return '<blockquote>Invalid quote!</blockquote>'; + } + + $id = $id + 1; + + return '<blockquote><a href="#' . $id .'">Quote from ' . $reply['user_name'] . '</a><br>' . $reply['post_content'] . '</blockquote>'; + } +} + +function display_posts($dbc, $sql_result) { + echo '<table>'; + $post_index = 1; + $thread_id = $_GET['id']; + + while ($row = mysqli_fetch_assoc($sql_result)) { + echo '<tr><th></th><th>' . $post_index . '</th></tr>'; + echo '<tr class="post" id=' . $post_index . '><td>Posted by <a href="user.php?id='. $row['user_id'] .'">' . $row['user_name'] . '</a><br><small>' . date('m/d/Y g:ia', strtotime($row['post_date'])) . '</small></td>'; + + $post_content = $row['post_content']; + + $post_content = preg_replace_callback('/>#\d+/', function($matches) use($thread_id, $dbc) { + return add_quote($dbc, $thread_id, $matches); + }, $post_content); + + // Replace YouTube URLs with embedded YouTube videos. + $post_content = preg_replace( + "/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i", + '<br><iframe class="youtube-embed" src="//www.youtube.com/embed/$2" allowfullscreen></iframe>', $post_content); + // Replace Image URLs with embedded images. + $post_content = preg_replace('@\b(http(s)?://)([^\s]*?(?:\.[a-z\d?=/_-]+)+(?:\.jpg|\.png|\.gif))(?![^<]*?(?:</\w+>|/?>))@i', '<img class="image-embed" src="http$2://$3" alt="http$2://$3" />', $post_content); + // Replace other URLs with links. + $post_content = preg_replace('@\b(http(s)?://)([^\s]*?(?:\.[a-z\d?=/_-]+)+)(?![^<]*?(?:</\w+>|/?>))@i', '<a href="http$2://$3">$0</a>', $post_content); + + echo '<td class="post-content">' . $post_content . '</td></tr>'; + + $post_index++; + } + echo '</table>'; +}
\ No newline at end of file |