alert("You must be logged in to post, redirecting.."); window.location.href = "login.php"; '; exit(); } // If the 'email' variable was set, no redirect, store other data $email = $_SESSION["email"]; $date_posted = date("Y-m-d H:i:s e"); // Insert relevant data in to database $sql = "INSERT INTO blog (title, content, email, date_posted) VALUES ('$title', '$content', '$email', '$date_posted')"; // Throw an error if the connection isn't established if ($conn->query($sql) === true) { } else { echo "Error: " . $sql . "
"; } } } // Aquire all saved posts from the database $sql = "SELECT date_posted, title, content FROM blog ORDER BY date_posted DESC"; $result = mysqli_query($conn, $sql); $sql_filter = ""; // Performs following function when blogs are retrieved from database if (isset($_GET["month"])) { // Assign the month to the variable month $month = $_GET["month"]; // Only show results for x month $sql_filter = " WHERE MONTH(date_posted) = '$month'"; } // Gets data from x month using the prior query $sql = "SELECT date_posted, title, content FROM blog" . $sql_filter . " ORDER BY date_posted DESC"; // Variable to store results $result = mysqli_query($conn, $sql); ?>
    0) { // Create an empty array to store the rows $rows = []; // Loop through each row returned from the database while ($row = mysqli_fetch_assoc($result)) { // Add the row to the array of rows $rows[] = $row; } // Sort the rows in descending order based on the date posted for ($i = 0; $i < count($rows) - 1; $i++) { for ($j = 0; $j < count($rows) - $i - 1; $j++) { if ($rows[$j]["date_posted"] < $rows[$j + 1]["date_posted"]) { // Swap the positions of the two rows $temp = $rows[$j]; $rows[$j] = $rows[$j + 1]; $rows[$j + 1] = $temp; } } } // Loop through each row and display it as a list item foreach ($rows as $row) { echo "
  • "; echo '

    ' . $row["date_posted"] . "

    "; echo '

    ' . $row["title"] . "

    "; echo '

    ' . $row["content"] . "

    "; echo "
  • "; } } else { // If no rows were returned from the database, display a message echo "No blog posts found."; } ?>