2.8. Displaying Forums and Posts

Both add_forum.php and add_post.php redirect the user to view.php after saving their data to the database. There are three different displays this file can generate: a list of all forums, a list of message threads that make up a particular forum and finally the contents of the messages. By passing parameters in the URL and analyzing them, you are able to determine which display should be generated. Briefly consider these examples:

  • view.php displays the list of all available forums.

  • view.php?fid=1 displays the threads in whatever forum has a primary key 1 in the database.

  • view.php?fid=1&mid2 displays the actual messages that make up thread 2 in forum 1.

Both fid and mid should be validated with each page call, but the database queries can also retrieve information at the same time, such as the forum name and how to construct back-links, if the parameters are valid to show to the user.

$forum_id = (isset($_GET['fid'])) ? (int)$_GET['fid'] : 0; $msg_id = (isset($_GET['mid'])) ? (int)$_GET['mid'] : 0; if ($forum_id) { $query = sprintf('SELECT FORUM_NAME FROM %sFORUM WHERE FORUM_ID = %d', DB_TBL_PREFIX, $forum_id); $result = mysql_query($query, $GLOBALS['DB']); if (!mysql_num_rows($result)) { die('<p>Invalid forum id.</p>'); } $row = mysql_fetch_assoc($result); echo '<h1>' . htmlspecialchars($row['FORUM_NAME']) . '</h1>'; mysql_free_result($result); if ($msg_id) { $query = sprintf('SELECT MESSAGE_ID FROM %sFORUM_MESSAGE ' . 'WHERE MESSAGE_ID = %d', DB_TBL_PREFIX, ...

Get PHP and MySQL®: Create-Modify-Reuse now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.