Skip to content

Commit b3e5b1f

Browse files
committed
Users are now able to view posts without having an account or being logged in, they are also able to search the posts
1 parent 39a69db commit b3e5b1f

File tree

5 files changed

+182
-2
lines changed

5 files changed

+182
-2
lines changed

about.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,22 @@
1818
<div class="navigationBar" id="navigationBar">
1919
<a href="index.php" class="active">Home</a>
2020
<a href="createAccount.php" class="active">Create account</a>
21-
<a href="index.php" class="active">Login</a>
21+
<div class="dropdown">
22+
<button class="dropbtn">Topics
23+
<i class="fa fa-caret-down"></i>
24+
</button>
25+
<div class="dropdown-content">
26+
<a href="viewNoAccount.php?mode=get&topic=all">All topics</a>
27+
<a href="viewNoAccount.php?mode=get&topic=softwareEngineering">Software engineering</a>
28+
<a href="viewNoAccount.php?mode=get&topic=computing">Computing</a>
29+
<a href="viewNoAccount.php?mode=get&topic=networks">Networks</a>
30+
<a href="viewNoAccount.php?mode=get&topic=cyberSecurity">Cyber security</a>
31+
<a href="viewNoAccount.php?mode=get&topic=bestPractices">Best practices</a>
32+
<a href="viewNoAccount.php?mode=get&topic=methods">Methods</a>
33+
<a href="viewNoAccount.php?mode=get&topic=tools">Tools</a>
34+
<a href="viewNoAccount.php?mode=get&topic=other">Other</a>
35+
</div>
36+
</div>
2237
<div class="loginContainer">
2338
<form action="login.php" method="POST"> <!-- The login in form for users with POST http method -->
2439
<input type="text" placeholder="Username" name="username">

createAccount.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,22 @@
4141
<div class="navigationBar" id="navigationBar">
4242
<a href="index.php" class="active">Home</a>
4343
<a href="about.php" class="active">About us</a>
44-
<a href="index.php" class="active">Login</a>
44+
<div class="dropdown">
45+
<button class="dropbtn">Topics
46+
<i class="fa fa-caret-down"></i>
47+
</button>
48+
<div class="dropdown-content">
49+
<a href="viewNoAccount.php?mode=get&topic=all">All topics</a>
50+
<a href="viewNoAccount.php?mode=get&topic=softwareEngineering">Software engineering</a>
51+
<a href="viewNoAccount.php?mode=get&topic=computing">Computing</a>
52+
<a href="viewNoAccount.php?mode=get&topic=networks">Networks</a>
53+
<a href="viewNoAccount.php?mode=get&topic=cyberSecurity">Cyber security</a>
54+
<a href="viewNoAccount.php?mode=get&topic=bestPractices">Best practices</a>
55+
<a href="viewNoAccount.php?mode=get&topic=methods">Methods</a>
56+
<a href="viewNoAccount.php?mode=get&topic=tools">Tools</a>
57+
<a href="viewNoAccount.php?mode=get&topic=other">Other</a>
58+
</div>
59+
</div>
4560
</div>
4661
<h1>Create account</h1>
4762
<div>

index.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@
2121
<div class="navigationBar" id="navigationBar">
2222
<a href="about.php" class="active">About</a>
2323
<a href="createAccount.php" class="active">Create Account</a>
24+
<div class="dropdown">
25+
<button class="dropbtn">Topics
26+
<i class="fa fa-caret-down"></i>
27+
</button>
28+
<div class="dropdown-content">
29+
<a href="viewNoAccount.php?mode=get&topic=all">All topics</a>
30+
<a href="viewNoAccount.php?mode=get&topic=softwareEngineering">Software engineering</a>
31+
<a href="viewNoAccount.php?mode=get&topic=computing">Computing</a>
32+
<a href="viewNoAccount.php?mode=get&topic=networks">Networks</a>
33+
<a href="viewNoAccount.php?mode=get&topic=cyberSecurity">Cyber security</a>
34+
<a href="viewNoAccount.php?mode=get&topic=bestPractices">Best practices</a>
35+
<a href="viewNoAccount.php?mode=get&topic=methods">Methods</a>
36+
<a href="viewNoAccount.php?mode=get&topic=tools">Tools</a>
37+
<a href="viewNoAccount.php?mode=get&topic=other">Other</a>
38+
</div>
39+
</div>
2440
<div class="loginContainer">
2541
<form action="" method="POST"> <!-- The login in form for users with POST http method -->
2642
<input type="text" placeholder="Username" name="username">

view.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
if (!isset($_COOKIE[$_SESSION['user_name']])) { // Checks if a users does not have a cookie in their browser
77
header('Location: index.php'); // Redirects the user to the index page (index.php)
88
} else {
9+
header('Location: viewNoAccount.php?mode=get&topic=all'); // Redirects the user to the view page for users that aren't logged in (viewNoAccount.php)
910
}
1011

1112
$urlTopic = $_GET['topic']; // Declares the variable with the value it gets from the post topic from the url

viewNoAccount.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)