Creating an Administration Area for a Simple Threaded Discussion Forum

Discussion forums usually need an administrator to handle various tasks that you wouldn’t want just any forum user doing, such as banning other users. This article, the second of two parts, will show you how to set up an administration area for a threaded discussion forum, and some of the functions you might want an administrator to manage.


Following my previous article titled “Creating a Threaded Discussion Forum,” we are going to create the administration side of things. Why do we need a administrator? Well, somebody has to manage the forum, and I guess the administrator drew the short straw. On the plus side the administrator can, among other things, ban a user from the forum, delete user entries and even censor the forum to some extend. We are going to implement the following functions:

  • Remove/add certain (bad) words.
  • Retrieve all topics.
  • Delete all topics.
  • Start a new topic.

To follow along with this part of the article you will need access to a MySQL database (created in the previous article) and any version of PHP above version 4.

How the administration area works

Basically, everything is going to be done on one page, which is the “siadmin.php” page. The navigation bar will be on the left with all its links and the main area will be where the outcome of a function will be displayed. The links will all send an action variable, which will be processed by the “switch” function. The action variable will be in a format like this:

<a href=”<? echo $_SERVER[‘PHP_SELF’] ?>?
action=deleteall”>Delete ALL Topics</a>

The “$_SERVER[‘PHP_SELF’]” is part of a global array of variables that is available to every PHP script. The PHP_SELF refers to the page on which the code is written. This ensures that the action variable is sent to the siadmin.php page.

The value of the action variable will be handled by the switch statement.  The switch statement has the following syntax:

Case “condition1”

Because it can handle a lot of different conditions in a clear and precise way, it is ideally suited to process the five links that we have in our navigation section. I use only one page for the administration because otherwise I would have had to create a page for each function. That would have required far more coding and waste valuable processing power on the server.

{mospagebreak title=Code}

Fig. 1 The administration page.

If you look to the navigation bar on the left, you will see that there are six options available, of which we will only implement five. The User Data  option involves a tracking system that we will not discuss in this article, so I have decided to leave it out for now. I’ve left the top bar where it says “Administrator:” empty in case you want to implement a login system that will enable you to show an admin name there. The text that you see in the main area comes from the default section of the switch statement. This section is executed if the variable that is being checked does not have any value in it or when the value does not match any of the conditions that have been set.

The remaining links will be implemented by the following functions:

  • function delall() – Deletes all the topics in the database.
  • function getall() – Retrieves all the main topics in the database.
  • function deltopic($uid) – Deletes a specific topic.
  • function showform() – Show a form to take words that you want banned.
  • function writeword() – Processes the above form.
  • function getword() -Displays the banned words.

Let’s talk about each of the functions in detail.

Fig. 2 The listing of the topics.

function getall(){
forumdb();//gets the database connection settings
echo ‘<table width=”360″ >’;
echo “<tr bgcolor=”#3399CC”>
<td width=”180″><b> Subject </b></td>
<td width=”180″><b>Record number</b><br></td>”;
echo ‘</tr>’;
echo ‘</table>’;
// If current page number, use it
// if not, set one!
    $page = 1;
} else {
    $page = $_GET[‘page’];
// Define the number of results per page
$max_results = 10;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) – $max_results);
// Perform MySQL query on only the current page number’s results
$sql = mysql_query(“Select * from test where parent = 0 LIMIT
$from, $max_results”);
//$query=”Select ip,page FROM tracker order by date”;
while($row = mysql_fetch_array($sql)){
    // Build your formatted results here.
    $Recid= $row[‘uid’];
echo ‘<table width=”360″>’;
echo ‘<tr>
<td width=”180″>’ .$title. ‘</td>
<td width=”180″>’.$Recid.’ <a href=”‘ .$_SERVER[‘PHP_SELF’]. ‘?
id=’ .$Recid. ‘&action=deltopics”>Delete</a><br></td>’;
echo ‘</tr>’;
echo ‘</table>’;
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query(“SELECT COUNT(*) as Num
FROM test”),0);
// Figure out the total number of pages. Always round up using
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo “<center>Select a Page<br />”;
// Build Previous Link
if($page > 1){
    $prev = ($page – 1);
    echo ‘<a href=”‘.$_SERVER[‘PHP_SELF’].’?
for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo “$i&nbsp;”;
        } else {
            echo ‘<a href=”‘.$_SERVER[‘PHP_SELF’].’?
// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo ‘<a href=”‘.$_SERVER[‘PHP_SELF’].’?page=’.$next.’&action=gettopics”>Next>></a>’;
echo “</center>”;

The point of this function is to retrieve ALL the records in the database. Naturally you would want to display only a few records at a time. To this end, the function also includes pagination code. Also, the function presents two columns to you; one contains the title of the record and the other contains an option to delete the record. Once you click on delete, the record is removed and the list is updated and displayed.

{mospagebreak title=The delall() and deltopic($uid) functions}

The delall() function is pretty straightforward. It deletes all the records in the table.

function delall(){
$query=”Delete * from test”;
echo ‘Could not delete records because ‘ .mysql_error() . ”;
echo ‘All records have been delete’;

The deltopic($uid) function takes the uid of the record and uses that ID to delete it.

function deltopic($uid){
$dbh=mysql_connect($host) or die (‘I cannot connect to the
database because: ‘ . mysql_error());
mysql_select_db ($dbname) or die(‘I cannot select the database
because: ‘ . mysql_error());
$query=”Delete from test where uid=$uid Limit 1″;
echo “Record number <b>$uid</b> deleted”;
echo “Could not delete record number <b>$uid</b> because
” .mysql_error() . “”;

In this function I’ve played it save and added the “Limit 1” statement to the delete query. This ensures that only one record is deleted and not all of them.

{mospagebreak title=Database connection, bad words}

The function below sets the database connection details:

function forumdb(){
$dbh=mysql_connect($host) or die (‘I cannot connect to the
database because: ‘ . mysql_error());
mysql_select_db ($dbname) or die(‘I cannot select the database
because: ‘ . mysql_error());

Remember to add your connection details to the this function.

This function shows a form that will enable you to enter a word that you want banned from the forum.

function showfrm(){
echo ‘<center> <form name=”form1″ method=”post” action=”‘ .$_SERVER[‘PHP_SELF’].’?action=writewords”> ‘;
echo ‘Enter word to ban:<br>
    <input type=”text” name=”word”><br>
    <input type=”Submit” name=”Submit” value=”Submit”
class=”altButtonFormat”> ‘;
 echo ‘</center></form>’;
 echo ‘<center><font color=”#FF0000″>Banned words will be replace
with XXXX.</font></center>’;

Here’s what the form looks like:


Fig. 3 The form is presented when you want a new word to be added to the banned word list.

The “getword()” function retrieves all the banned words:

function getword(){
echo “<center><b><h3>Banned Words</h3></b></center>”;
echo “<center>———————</center>”;
for($i=0; $i < count($words); $i++){
echo “<center>$words[$i]</center><br>”;

The function interacts with a text file on the disk and uses PHP’s very handy file handling functions to retrieve a list of banned words. Here’s a screen shot of the above function result:

Fig. 4 A list of  banned words.

The writeword() function writes all the words to a file called “badwords.txt.”

function writeword(){
if($fp=fopen(‘badwords.txt’, ‘a+’)){
fwrite($fp, “$wordrn”);
echo “<center>The word <b>$word</b> has now been
echo “could not open file”;

As I mentioned before we will use a $action variable to receive the value passed from the link that has been clicked, and then use the switch command to handle the value accordingly. Here’s a snippet of the switch command:

    Case “gettopics”;
  Case “deleteall”;

As the code above shows, once the user clicks on the “get all main topics” link the “getall();” function is called. The same thing happens when the user clicks on the “Delete all topics” link; the “delall()” function is called.


That’s it for the admin section of the forum. Here are a few suggestions to improve its performance: create a table to store the “bad words.” This will enable you to add/remove words more easily than you would with a file-based system. Try to set up a login script to control access to the admin area, as I’m sure you would not want just anyone to have access to the functions on that page!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye