|
| 1 | +<!DOCTYPE html> |
| 2 | +<?php |
| 3 | +include "databaseConnection.php"; // Includes the databaseConnection.php script to allow this script to access the database |
| 4 | + |
| 5 | +if (isset($_COOKIE[$_SESSION['user_name']])) { // Checks if a users does have a cookie in their browser |
| 6 | + header('Location: userHome.php'); // Redirects the user to the user home page (userHome.php) |
| 7 | +} else { |
| 8 | +} |
| 9 | + |
| 10 | +$urlTopic = $_GET['topic']; // Declares the variable with the value it gets from the post topic from the url |
| 11 | + |
| 12 | +if ($urlTopic === "all") { // Checks if the topic from the URL is all |
| 13 | + $get_all_posts_query_string = "SELECT * FROM post_tbl"; // SQL query to get all posts from the database |
| 14 | +} else { |
| 15 | + if ($urlTopic === "bestPractices") { |
| 16 | + $get_all_posts_query_string = "SELECT * FROM post_tbl WHERE post_category = 'best practices'"; // SQL query to get best practices posts from the database |
| 17 | + } elseif ($urlTopic === "cyberSecurity") { |
| 18 | + $get_all_posts_query_string = "SELECT * FROM post_tbl WHERE post_category = 'cyber security'"; // SQL query to get cyber security posts from the database. |
| 19 | + } elseif ($urlTopic === 'softwareEngineering') { |
| 20 | + $get_all_posts_query_string = "SELECT * FROM post_tbl WHERE post_category = 'software engineering'"; // SQL query to get software engineering posts from the database. |
| 21 | + } else { |
| 22 | + $get_all_posts_query_string = "SELECT * FROM post_tbl WHERE post_category = '$urlTopic'"; // SQL query to get the posts from the database with the topic from the url |
| 23 | + } |
| 24 | +} |
| 25 | +$result = mysqli_query($connection, $get_all_posts_query_string); // Stores the result from the sql query as a variable |
| 26 | +//echo $urlTopic; // Used for development and testing |
| 27 | +?> |
| 28 | +<html lang="eng"> |
| 29 | +<head> |
| 30 | + <title>View posts</title> |
| 31 | + <link rel="stylesheet" href="static/css/view.css"> |
| 32 | + <link rel="stylesheet" href="static/css/navigationBar.css"> |
| 33 | +</head> |
| 34 | +<body> |
| 35 | +<div class="navigationBar" id="navigationBar"> |
| 36 | + <a href="index.php" class="active">Home</a> |
| 37 | + <a href="about.php" class="active">About</a> |
| 38 | + <a href="createAccount.php" class="active">Create Account</a> |
| 39 | + <div class="dropdown"> |
| 40 | + <button class="dropbtn">Topics |
| 41 | + <i class="fa fa-caret-down"></i> |
| 42 | + </button> |
| 43 | + <div class="dropdown-content"> |
| 44 | + <a href="viewNoAccount.php?mode=get&topic=all">All topics</a> |
| 45 | + <a href="viewNoAccount.php?mode=get&topic=softwareEngineering">Software engineering</a> |
| 46 | + <a href="viewNoAccount.php?mode=get&topic=computing">Computing</a> |
| 47 | + <a href="viewNoAccount.php?mode=get&topic=networks">Networks</a> |
| 48 | + <a href="viewNoAccount.php?mode=get&topic=cyberSecurity">Cyber security</a> |
| 49 | + <a href="viewNoAccount.php?mode=get&topic=bestPractices">Best practices</a> |
| 50 | + <a href="viewNoAccount.php?mode=get&topic=methods">Methods</a> |
| 51 | + <a href="viewNoAccount.php?mode=get&topic=tools">Tools</a> |
| 52 | + <a href="viewNoAccount.php?mode=get&topic=other">Other</a> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | +</div> |
| 56 | +<div class="searchContainer"> |
| 57 | + <form action="viewNoAccount.php" class="userSearch" method="POST"> <!-- Search bar for searching posts --> |
| 58 | + <input type="text" placeholder="search 🔍" name="searchQuery" class="userSearch"> |
| 59 | + <input type="submit" name="searchPosts" value="search"> |
| 60 | + </form> |
| 61 | +</div> |
| 62 | +<?php |
| 63 | +if (isset($_POST['searchPosts'])) { // Checks if a user has submitted a form with a POST request method from the form 'searchPosts' |
| 64 | + $searchQuery = $_POST['searchQuery']; // Declares the variable $searchQuery with the data that the user entered into the searchPosts form |
| 65 | + $search_query_string = "SELECT * FROM post_tbl WHERE post_title LIKE '%$searchQuery%'"; // The $search_query_string variable is declared with the select SQL query with the users search value |
| 66 | + |
| 67 | + //echo $search_query_string; // Used for development and testing |
| 68 | + |
| 69 | + $searchResult = mysqli_query($connection, $search_query_string); // Stores the result from the database from the SQL query |
| 70 | + |
| 71 | + $count = mysqli_num_rows($searchResult); // Gets the amount of rows returned from the database |
| 72 | + |
| 73 | + if ($count == 0) { // Checks if the amount of rows returned from the database is equal to zero |
| 74 | + echo "<h3>There are no posts that match your search</h3>"; // Outputs a message |
| 75 | + } elseif ($count > 0) { // Checks if the amount of rows returned from the database is more than zero |
| 76 | + while ($row = mysqli_fetch_assoc($searchResult)) { // Goes through all of the rows of data returned from the database |
| 77 | + |
| 78 | + $postId = $row['post_id']; // Declares the variable with the values returned from the database |
| 79 | + $postUserId = $row['user_id']; // ^ same as the line above |
| 80 | + $postTitle = $row['post_title']; // ^ same as the line above |
| 81 | + $postDate = $row['post_date']; // ^ same as the line above |
| 82 | + $postEditDate = $row['post_edit_date']; // ^ same as the line above |
| 83 | + $postContent = $row['post_content']; // ^ same as the line above |
| 84 | + $postCategory = $row['post_category']; // ^ same as the line above |
| 85 | + |
| 86 | + echo "<ul>"; // Echos a list containing the data that was returned by the database |
| 87 | + echo "<p class='postTitle'>" . $postTitle . "</p>"; |
| 88 | + echo "<li>Content: " . $postContent . "</li>"; |
| 89 | + echo "<li>Created: " . $postDate . "</li>"; |
| 90 | + if (isset($postEditDate)) { // Checks to see if the post has been edited |
| 91 | + echo "<li>Post edited on: " . $postEditDate . "</li>"; |
| 92 | + } else { |
| 93 | + } |
| 94 | + echo "<li>Category: " . $postCategory . "</li>"; |
| 95 | + echo "</ul>"; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +if (mysqli_query($connection, $get_all_posts_query_string)) { |
| 101 | + echo '<script>console.log("Posts received");</script>'; |
| 102 | +} else { |
| 103 | + echo '<script>console.log("Posts not received");</script>'; |
| 104 | +} |
| 105 | +$count = mysqli_num_rows($result); // Gets the amount of rows returned from the database |
| 106 | + |
| 107 | +if ($count == 0) { |
| 108 | + echo "<h3>There are no posts for that category</h3>"; |
| 109 | +} elseif ($count > 0) { |
| 110 | + while ($row = mysqli_fetch_assoc($result)) { // Goes through all of the rows of data returned from the database |
| 111 | + $postId = $row['post_id']; // Declares the variable with the values returned from the database |
| 112 | + $postUserId = $row['user_id']; // ^ same as the line above |
| 113 | + $postTitle = $row['post_title']; // ^ same as the line above |
| 114 | + $postDate = $row['post_date']; // ^ same as the line above |
| 115 | + $postEditDate = $row['post_edit_date']; // ^ same as the line above |
| 116 | + $postContent = $row['post_content']; // ^ same as the line above |
| 117 | + $postCategory = $row['post_category']; // ^ same as the line above |
| 118 | + |
| 119 | + echo "<ul>"; // Echos a list containing the data that was returned by the database |
| 120 | + echo "<p class='postTitle'>" . $postTitle . "</p>"; |
| 121 | + echo "<li>Content: " . $postContent . "</li>"; |
| 122 | + echo "<li>Created: " . $postDate . "</li>"; |
| 123 | + if (isset($postEditDate)) { // Checks to see if the post has been edited |
| 124 | + echo "<li>Post edited on: " . $postEditDate . "</li>"; |
| 125 | + } else { |
| 126 | + } |
| 127 | + echo "<li>Category: " . $postCategory . "</li>"; |
| 128 | + echo "</ul>"; |
| 129 | + } |
| 130 | +} |
| 131 | +?> |
| 132 | +</body> |
| 133 | +</html> |
0 commit comments