diff options
| author | Cflip <36554078+cflip@users.noreply.github.com> | 2021-01-23 21:39:56 -0700 | 
|---|---|---|
| committer | Cflip <36554078+cflip@users.noreply.github.com> | 2021-01-23 21:39:56 -0700 | 
| commit | d55089758306fd078a00d2a6d9d0fafc14a0edcf (patch) | |
| tree | 0d000652391eebc9e18d3737794983bb72bfd6bc | |
| parent | 93825beae2dd7cdcaca8bdb3aace712aee85fe36 (diff) | |
Implement latest post/thread columns
| -rw-r--r-- | category.php | 22 | ||||
| -rw-r--r-- | index.php | 23 | 
2 files changed, 37 insertions, 8 deletions
| diff --git a/category.php b/category.php index fcc7851..f5cf9cb 100644 --- a/category.php +++ b/category.php @@ -27,23 +27,37 @@ mysqli_free_result($result);  echo '</section>'; -$sql = "SELECT thread_id, thread_subject, thread_date, user_name FROM threads LEFT JOIN users ON thread_author = user_id WHERE thread_cat = " . mysqli_real_escape_string($dbc, $_GET['id']) . " ORDER BY thread_id DESC"; +$sql = "SELECT thread_id, thread_subject, thread_date, user_name FROM threads JOIN users ON thread_author = user_id WHERE thread_cat = " . mysqli_real_escape_string($dbc, $_GET['id']) . " ORDER BY thread_id DESC";  $result = mysqli_query($dbc, $sql);  if (!$result) {  	die('Error trying to display threads: ' . mysqli_error($dbc));  }  -// Display table of posts -  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>' . $row['user_name'] . '</b> on ' . date('M d, Y', strtotime($row['thread_date'])) . '</small></td><td class="right">by <b>cflip</b><br><small>01-22-2021 9:34</small></td></tr>'; +	echo '<small>by <b>' . $row['user_name'] . '</b> on ' . date('M d, Y', strtotime($row['thread_date'])) . '</small></td><td class="right">by <b>' . $thread['user_name'] . '</b><br><small>' . date('m/d/Y g:ia', strtotime($thread['post_date'])) . '</small></td></tr>';  } +mysqli_stmt_close($stmt); +mysqli_free_result($result); +  echo '</table>';  include 'footer.php';
\ No newline at end of file @@ -24,25 +24,40 @@ include_once 'header.php';  		die('Failure trying to display categories: ' . mysqli_error($dbc));  	} +	$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($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']; -		echo '</td><td class="right">Example thread<br><small>1 hour ago by <b>example user</b></small></td></tr>'; +		echo '</td><td class="right">' . $thread['thread_subject'] . '<br>'; +		echo '<small>by <b>' . $thread['user_name'] . '</b></small></td></tr>';  	} +	mysqli_stmt_close($stmt);  	mysqli_free_result($result);  ?>  	</table>  	<table>  		<tr>  			<th class="left">Latest Threads</th> -			<th class="right">Latest Post</th> +			<th class="right">Categories</th>  		</tr>  <?php  	include_once 'includes/db_inc.php'; -	$sql = "SELECT thread_id, thread_subject, thread_date, user_name FROM threads LEFT JOIN users ON thread_author = user_id ORDER BY thread_id DESC LIMIT 5"; +	$sql = "SELECT thread_id, thread_subject, thread_date, 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) { @@ -52,7 +67,7 @@ include_once 'header.php';  	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>' . $row['user_name'] . '</b> on ' . date('M d, Y', strtotime($row['thread_date'])) . '</small></td><td class="right">by <b>cflip</b><br><small>01-22-2021 9:34</small></td></tr>'; +		echo '<small>by <b>' . $row['user_name'] . '</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>';  	}  	mysqli_free_result($result); | 
