From 3b0349188c83b24cb8ff7dfe76796dabc3916e83 Mon Sep 17 00:00:00 2001 From: cflip <36554078+cflip@users.noreply.github.com> Date: Wed, 7 Apr 2021 16:24:54 -0600 Subject: Styling and formatting for posts --- model/Post.php | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'model/Post.php') diff --git a/model/Post.php b/model/Post.php index ef2fe4f..02c9dec 100644 --- a/model/Post.php +++ b/model/Post.php @@ -2,6 +2,28 @@ include_once 'Thread.php'; +function add_quote($dbc, $thread_id, $matches) { + foreach ($matches as $match) { + $id = (int) filter_var($match, FILTER_SANITIZE_NUMBER_INT); + $sql = "SELECT post_content, post_author, user_name FROM posts LEFT JOIN users ON post_author = user_id WHERE post_id = " . $id; + $result = mysqli_query($dbc, $sql); + + if (!$result) { + return '
'; + } + + $reply = mysqli_fetch_assoc($result); + + if (empty($reply)) { + return '
Invalid quote!
'; + } + + $id = $id + 1; + + return '
Quote from ' . $reply['user_name'] . '
' . $reply['post_content'] . '
'; + } +} + class Post { public $id; public $content; @@ -36,14 +58,15 @@ class Post { mysqli_free_result($result); } - function display_content() { - echo '
#' . $this->id . ' Posted by ' . $this->author->name . ' on ' . date('m/d/Y g:ia', strtotime($this->date)) . '
'; + function display_content($dbc) { + echo '
#' . $this->id . ' Posted by ' . $this->author->name . ' on ' . date('m/d/Y g:ia', strtotime($this->date)) . '
'; $post_content = $this->content; + $thread_id = $this->id; - // $post_content = preg_replace_callback('/>#\d+/', function($matches) use($thread_id, $dbc) { - // return add_quote($dbc, $thread_id, $matches); - // }, $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( @@ -54,7 +77,7 @@ class Post { // Replace other URLs with links. $post_content = preg_replace('@\b(http(s)?://)([^\s]*?(?:\.[a-z\d?=/_-]+)+)(?![^<]*?(?:|/?>))@i', '$0', $post_content); - echo '

' . $post_content . '

'; + echo '' . $post_content . ''; } } -- cgit v1.2.3