Added forum category adding

This commit is contained in:
Samuel Ortion 2021-03-30 10:52:22 +02:00
parent 0a06add2c4
commit 8192e30810
8 changed files with 202 additions and 5 deletions

View File

@ -0,0 +1,49 @@
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require "$root/database/credentials.php";
// Connect the database
try{
$db = new PDO("mysql:host=$host;dbname=$database;charset=utf8",
$user,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
}catch (Exception $e){
die("Error : ".$e->getMessage());
}
$req = $db->prepare('SELECT * FROM `categories`');
$req->execute();
$rows = $req->fetchAll(PDO::FETCH_ASSOC);
?>
<h3>Categories</h3>
<table id="categories">
<thead>
<tr>
<th scope="col">Title</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<?php
foreach ($rows as $row) {
?>
<tr>
<td><?=$row['cat_name']?></td>
<td><?=$row['cat_description']?></td>
</tr>
<?php
}
?> </tbody>
</table>
<h3>Create a category</h3>
<form action="createcategory.php" method="post">
<label for="title">Category title</label>
<input type="text" id="title" name="title" placeholder="Enter a name for the new category.." required>
<label for="description">Category description</label>
<textarea name="description" id="description" cols="30" rows="10" placeholder="Enter a description for the new category.." required></textarea>
<input type="submit" value="submit" name="submit"><input type="reset" value="reset">
</form>

View File

@ -0,0 +1,62 @@
<?php
function create_category($name, $description)
{
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require "$root/database/credentials.php";
// Connect the database
try{
$db = new PDO("mysql:host=$host;dbname=$database;charset=utf8",
$user,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
}catch (Exception $e){
die("Error : ".$e->getMessage());
}
$req = $db->prepare('INSERT INTO `categories` (cat_name, cat_description) VALUES (:cat_name, :cat_description)');
$req->execute(array(
"cat_name"=>$name,
"cat_description"=>$description
));
}
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
$_SESSION['error_msg'] = "";
if (isset($_POST['submit']))
{
if (isset($_SESSION['username'])) {
if (isset($_POST['title']))
{
$name = $_POST['title'];
} else
{
$_SESSION['error_msg'] .= "You did not enter a proper category title. \n";
}
if (isset($_POST['description']))
{
$description = $_POST['description'];
} else
{
$_SESSION['error_msg'] .= "You did not enter a proper category description. \n";
}
} else {
$_SESSION['error_msg'] .= "You did not log in.\n";
header('Location: '."/auth/login/");
}
} else
{
$_SESSION['error_msg'] .= "You did not sumit the category creation form.\n";
}
if ($_SESSION['error_msg'] == "")
{
create_category($name, $description);
header('Location: '."/forum");
} else {
header('Location: '."/auth/login/");
}
?>

29
public/forum/index.php Normal file
View File

@ -0,0 +1,29 @@
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chiro - Canto</title>
<link rel="stylesheet" type="text/css" href="/styles/style.css">
</head>
<body>
<?php include("$root/menu.php");?>
<?php include("$root/header.php");?>
<section>
<h2>Forum</h2>
<?php include("categories.php");?>
</section>
<?php include("$root/footer.php");?>
</body>
<script src="/scripts/script.js"></script>
</html>

View File

@ -0,0 +1,32 @@
<?php
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require "$root/database/credentials.php";
// Connect the database
try {
$db = new PDO("mysql:host=$host;dbname=$database;charset=utf8",
$user,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
} catch (Exception $e) {
die("Error : ".$e->getMessage());
}
$req = $db->prepare('SELECT id, recordist_name, file_name, license, species, sound_type, date, time FROM `records` ORDER BY `entry_timestamp` ASC LIMIT 1');
$req->execute();
if ($data = $req->fetch())
{
?>
<div class="sound">
<h3><?=$data['file_name']?></h3>
<h4><em><?=$data['species']?></em></h4>
<p>Recorded on <?=$data['date']?> at <?=$data['time']?></p>
<audio src="<?="../storage/records/".$data['file_name']?>" controls></audio>
<p><?=$data['license']?> <?=$data['recordist_name']?></p>
</div>
<?php
} else {
echo "Error fetch.";
}
?>

View File

@ -1,8 +1,10 @@
<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
?>
<!DOCTYPE html>
@ -19,6 +21,7 @@ session_start();
<?php include("header.php");?>
<section>
<h2>Last uploaded sound</h2>
<?php include("$root/home/last-uploaded-file.php"); ?>
</section>
<?php include("footer.php");?>
</body>

View File

@ -131,7 +131,7 @@ nav {
border-bottom: 1px solid black;
position: flex;
display: block;
width: 100vw;
width: 99vw;
background-color: white;
z-index: 10;
}
@ -208,6 +208,21 @@ p.large {
width: auto;
}
table {
border-radius: 5px;
width: 100%;
border-collapse: collapse;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 0.5em;
}
tr:nth-child(even) {
background-color: #dddddd;
}
#searchbar {
display: flex;
flex-direction: row;
@ -246,4 +261,11 @@ div.coordinates input[type="text"] {
border-radius: 5px;
padding: 0.5em;
background-color: rgba(255, 0, 0, 0.4)
}
.sound {
background-color: rgba(0, 255, 0, 0.5);
padding: 1em;
border-radius: 5px;
overflow: auto;
}

View File

@ -14,7 +14,7 @@ try {
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
}catch (Exception $e) {
} catch (Exception $e) {
die("Error : ".$e->getMessage());
}