<?php
//category.php
include 'connect.php';
include 'header.php';

//first select the category based on $_GET['cat_id']
$sql = "SELECT catNo, catName, catDescr
				FROM categories
				WHERE catNo = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $_GET['id']);
$stmt->execute();

$result = $stmt->get_result();

if(!$result)
{
	echo 'The category could not be displayed, please try again later.';
}
else
{
	if($result->num_rows == 0)
	{
		echo 'This category does not exist.';
	}
	else
	{
		//display category data
		while($row = $result->fetch_assoc())
		{
			echo '<h2>Topics in &prime;' . htmlentities($row['catName'], ENT_QUOTES | ENT_HTML5, 'UTF-8') . '&prime; category</h2><br />';
		}

		//do a query for the topics
		$sql = "SELECT topicNo, topicSubject, topicDate, topicCat
						FROM topics
						WHERE	topicCat = ?
						ORDER BY topicDate";

		$stmt = $conn->prepare($sql);
		$stmt->bind_param('i', $_GET['id']);
		$stmt->execute();

		$result = $stmt->get_result();

		if(!$result)
		{
			echo 'The topics could not be displayed, please try again later.';
		}
		else
		{
			if($result->num_rows == 0)
			{
				echo 'There are no topics in this category yet.';
			}
			else
			{
				//prepare the table
				echo '<table border="1">
					  <tr>
						<th>Topic</th>
						<th>Created at</th>
					  </tr>';

				while($row = $result->fetch_assoc())
				{
					echo '<tr>';
						echo '<td class="leftpart">';
						echo 	'<h3><a href="topic.php?id=' . htmlentities($row['topicNo'], ENT_QUOTES | ENT_HTML5, 'UTF-8') . '">' . htmlentities($row['topicSubject'], ENT_QUOTES | ENT_HTML5, 'UTF-8') . '</a><br /><h3>';
						echo '</td>';
						echo '<td class="rightpart">';
						echo	 date_format(date_create($row['topicDate']), 'd/m/Y H:i:s');
						echo '</td>';
					echo '</tr>';
				}

				//Close the table up
				echo '</table>';
			}
		}
	}
}

include 'footer.php';
?>
