We help IT Professionals succeed at work.

Using PHP to do a discussion forum

fanren
fanren asked
on
I am not familiar with PHP. But I need to create a discussion forum using PHP. Maybe the problem is more of a database design than using PHP.

I notice some forums allows seemingly many layers of replies eg, A
               B.1 (reply to A)
               B.2  (reply to A)
                 C   (reply to B.2)
                  D   (reply to C)
                   E   (reply to D)
                    F.1 (reply to E)
                    F.2 (reply to E)
My qn is : how can this be done? How should the database design be like? Do i need to create table dynamically everytime a different layer of reply is formed?
Comment
Watch Question

Commented:
the table:
ID  Descritpion  ParentID
1    A                     0
2    B                     1
3    C                     0
4    D                     3
5    E                      4

A
---B
C
---D
-------E

Commented:
you can have a look at the following and takes the idea

<?
class Forum
{
      var $Connectforum;
      
      var $compteurdim;
      var $compteurposition;
                  
      // store db connection
      function Forum($connect)
      {
        $this->Connectforum = $connect;
             
      }
            
      
      // ****** functions to manage the forum ******
      
      // add a forum with all info
      function addforum($forumname,$parentid,$desc="")
      {
       $desc = AddSlashes($desc);
      
       $query = "insert into ENTITY (PARENT,forum_name,forum_desc) values($parentid,'$forumname','$desc')";
       mysql_query($query,$this->Connectforum);
                    
       $query = "select ENTITY_ID from ENTITY order by ENTITY_ID desc";
       $newid = mysql_query($query,$this->Connectforum);
       $record = mysql_fetch_array($newid);

       return $record[ENTITY_ID];
      }
      
      
      // modify a forum
      function modifyforum($id,$pid,$name,$desc)
      {
       $desc = AddSlashes($desc);
      
       $query = "update ENTITY set PARENT=$pid,forum_name='$name',forum_desc='$desc' where ENTITY_ID=$id";       
       mysql_query($query,$this->Connectforum);
      
      }


      // delete a forum
      function eraseforum($id)
      {       
       $query = "delete from ENTITY where ENTITY_ID=$id";
       mysql_query($query,$this->Connectforum);

      }

      
        // test if there are child forum to delete a forum (1=child, 0 no child)
        function testdeleteforum($id)
        {
         $query = "select * from ENTITY where PARENT=$id";
         $childforum = mysql_query($query,$this->Connectforum);
         $firstforum = mysql_fetch_array($childforum);
       
         if(is_array($firstforum))
         {
          return 1;
         }
         else
         {
          return 0;
         }
        }
       
        // delete all the child forum from a forum pass as argument
        function deletechildforum($id)
        {
         
         $listforum = $this->searchdeletechild($id);
              
        }
       
       
        // search and delete child forum
        function searchdeletechild($id)
        {
         $query = "select * from ENTITY where PARENT=$id";
       $recordforum = mysql_query($query,$this->Connectforum);
       $firstforum = mysql_fetch_array($recordforum);
       
         
         while(is_array($firstforum))
       {
        $listforum=$this->searchdeletechild($firstforum[ENTITY_ID]);
        
        $firstforum = mysql_fetch_array($recordforum);
        
       }
      
       $query = "delete from ENTITY where ENTITY_ID=$id";
         mysql_query($query,$this->Connectforum);
             
        }

      // display a forum pass as argument
      function getoneforum($id)
      {
       $query = "select * from ENTITY where ENTITY_ID=$id";
       $recordforum= mysql_query($query,$this->Connectforum);
       $displayforum = mysql_fetch_array($recordforum);
      
       return $displayforum;
      }


      // display a forum + all the child
      function displaylistforum($id)
      {
       $query = "select * from ENTITY where ENTITY_ID=$id";
       $recordforum= mysql_query($query,$this->Connectforum);
       $displayforum = mysql_fetch_array($recordforum);
               
       $this->compteurdim=0;
       $this->compteurposition=0;
      
       $listforum[$this->compteurdim]=array(0 => $this->compteurposition,1 => $displayforum);
       $this->compteurposition=$this->compteurposition+1;
       $this->compteurdim=$this->compteurdim+1;
             
       // call the research child function
       $listforum = $this->searchchild($id,$listforum);
      
       return $listforum;
      }


      // recursive function to search all the child from a forum
      function searchchild($id,$listforum)
      {
       $query = "select * from ENTITY where PARENT=$id";
       $recordforum = mysql_query($query,$this->Connectforum);
       $firstforum = mysql_fetch_array($recordforum);
      
       while(is_array($firstforum))
       {
        $listforum[$this->compteurdim]=array(0 => $this->compteurposition,1 => $firstforum);
        $this->compteurposition=$this->compteurposition+1;
        $this->compteurdim=$this->compteurdim+1;
               
        $listforum=$this->searchchild($firstforum[ENTITY_ID],$listforum);
        $this->compteurposition=$this->compteurposition-1;
        $firstforum = mysql_fetch_array($recordforum);       
       }       
      
       return $listforum;
      }


      // retreive all the forum
      function getTree()
      {
       $query = "select * from ENTITY where PARENT=0";
       $recordforum= mysql_query($query,$this->Connectforum);
       $displayforum = mysql_fetch_array($recordforum);
      
       $this->compteurdim=0;
       $this->compteurposition=0;
                    
       while(is_array($displayforum))
       {
        $listforum[$this->compteurdim]=array(0 => $this->compteurposition,1 => $displayforum);
        $this->compteurposition=$this->compteurposition+1;
        $this->compteurdim=$this->compteurdim+1;
        $listforum = $this->searchchild($displayforum[ENTITY_ID],$listforum);
        $this->compteurposition=$this->compteurposition-1;
        $displayforum = mysql_fetch_array($recordforum);
       }
      
       return $listforum;      
      }
      
      
      // get the parent's forum of a forum passed as argument
      function getforumparent($id)
      {
       $query = "select * from ENTITY where PARENT=$id";
       $recordforum= mysql_query($query,$this->Connectforum);
             
       return mysql_fetch_array($recordforum);
      }
      
            
      // change the parent of a forum
      function setforumparent($id,$pid)
      {
       $query = "update ENTITY set PARENT=$pid where ENTITY_ID=$id";       
       mysql_query($query,$this->Connectforum);      
      }
}
?>

Commented:
Yes, the database design is the most important ingredient to get a threaded forum to work.

Basically you need to be able to relate messages to each other in a parent child fashion.  This is often called a self referential relationship.  This type of table design would allow you to build a basic functional forum.  I didn't include the User Table, but it would be of course required, and you would need some additional fields if you wanted to support thinks like aliased posts (ie, posts from non registered users):


Message
-----------
MessageId (Key)
ParentMessageId (Zero if it's a new Topic, otherwise, it's a reply to MessageId)
From (Key to User Table)
Subject (String)
MessageText (LongString)
Created (DateTime)




Eddie ShipmanAll-around developer
BRONZE EXPERT

Commented:
Why re-invent the wheel? Try Phorum or XMB, free,
open-source discussion boards.

Commented:
Phorum is really good but sometimes you need something more simple

Explore More ContentExplore courses, solutions, and other research materials related to this topic.