130 lines
3.8 KiB
PHP
130 lines
3.8 KiB
PHP
<?php
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
|
|
include("$root/../vendor/erusev/parsedown/Parsedown.php");
|
|
$Parsedown = new Parsedown();
|
|
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());
|
|
}
|
|
|
|
if (isset($_GET['topic'])){
|
|
$topic_id = $_GET['topic'];
|
|
$req = $db->prepare('SELECT topic_subject FROM topics WHERE id=:id');
|
|
$req->execute(array(
|
|
"id"=>$topic_id
|
|
));
|
|
if ($data = $req->fetch())
|
|
{
|
|
$subject = $data['topic_subject'];
|
|
}
|
|
$req = $db->prepare('SELECT * FROM replies WHERE reply_topic=:id_topic');
|
|
$req->execute(array(
|
|
"id_topic"=>$topic_id
|
|
));
|
|
try {
|
|
$rows = $req->fetchAll(PDO::FETCH_ASSOC);
|
|
?>
|
|
<h3><?=$subject?></h3>
|
|
<table id="replies">
|
|
<thead>
|
|
<tr>
|
|
<th scope="col">Author</th>
|
|
<th scope="col">Reply</th>
|
|
<th scope="col">Date</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php
|
|
foreach ($rows as $row) {
|
|
$reply_by = $row['reply_by'];
|
|
$req = $db->prepare('SELECT firstname, lastname, username FROM `authors` WHERE id=:user_id');
|
|
$req->execute(array(
|
|
"user_id"=>$reply_by
|
|
));
|
|
if ($data = $req->fetch())
|
|
{
|
|
$username = $data['username'];
|
|
$firstname = $data['firstname'];
|
|
$lastname = $data['lastname'];
|
|
}
|
|
?>
|
|
<tr>
|
|
<td><?="$firstname $lastname<br> ($username)"?></td>
|
|
<td><?=$Parsedown->text($row['reply_content'])?></td>
|
|
<td><?=$row['reply_date']?></td>
|
|
</tr>
|
|
<?php
|
|
}
|
|
?>
|
|
</tbody>
|
|
</table>
|
|
<br>
|
|
<form action="sendreply.php" method="post">
|
|
<input type="hidden" name="topic_id" value="<?=$topic_id?>">
|
|
<label for="reply">Your Reply</label>
|
|
<textarea name="reply" id="reply" cols="30" rows="10" placeholder="Enter your reply (support Markdown).."></textarea>
|
|
<input type="submit" name="submit" value="submit">
|
|
<input type="reset" name="reset" value="reset">
|
|
</form>
|
|
<?php
|
|
|
|
} catch (Exception $e)
|
|
{
|
|
die("Error : ".$e->getMessage());
|
|
echo "Can fetch topic data.\n";
|
|
}
|
|
}
|
|
else {
|
|
|
|
$req = $db->prepare('SELECT * FROM `topics`');
|
|
$req->execute();
|
|
$rows = $req->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
?>
|
|
<h3>Topics</h3>
|
|
<table id="categories">
|
|
<thead>
|
|
<tr>
|
|
<th scope="col">Subjects</th>
|
|
<th scope="col">Answers</th>
|
|
<th scope="col">Author</th>
|
|
<th scope="col">Creation date</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php
|
|
foreach ($rows as $row) {
|
|
$topic_by = $row['topic_by'];
|
|
$req = $db->prepare('SELECT firstname, lastname, username FROM `authors` WHERE id=:user_id');
|
|
$req->execute(array(
|
|
"user_id"=>$topic_by
|
|
));
|
|
if ($data = $req->fetch())
|
|
{
|
|
$username = $data['username'];
|
|
$firstname = $data['firstname'];
|
|
$lastname = $data['lastname'];
|
|
}
|
|
?>
|
|
<tr>
|
|
<td><a href="<?="?topic=".$row['id']?>"><?=$row['topic_subject']?></a></td>
|
|
<td><?="To be completed"?></td>
|
|
<td><?="$firstname $lastname ($username)"?></td>
|
|
<td><?=$row['topic_date']?></td>
|
|
</tr>
|
|
<?php
|
|
}
|
|
?> </tbody>
|
|
</table>
|
|
<?php
|
|
}
|