Jump to content

Recommended Posts

Good afternoon, I am attempting to create a custom forum for my own personal website which I'm working on but I can't really grasp the concept of how my database should be set up.

 

At the moment I have a 'forum_categories' table with 5 fields; id, forum, topics, posts, last_post.

 

I have put some dummy data into my table so that I can test displaying it.

 

I have attached 2 screenshots, first one is my database structure and the second one is the data.

 

The idea is that when they first visit the forum page, the topics and posts table columns should count the amount of topics and posts for each section and of course, last_post is self explanatory.

 

At this present time I'm not sure on what to do. I know I will need more tables holding the actual posts but I'm not sure how to properly link each topic to the correct section and how to link the topics with the posts.

 

Do I need 2 more tables, one say topics and one say posts and link them with ID's?

 

Is there a standard way to create forums or could anyone point me in the direction of credible tutorials?

 

I look forward to your help.

 

Kind regards,

 

L2c.

post-128658-0-70390400-1369762807_thumb.png

post-128658-0-00227200-1369762814_thumb.png

Edited by Love2c0de
Link to comment
https://forums.phpfreaks.com/topic/278482-creating-a-basic-forum/
Share on other sites

There is nothing complex about a forum. At the bare basic you have a Topic and a Post. Everything else are just categories, which in turn can have categories.

 

Instead of starting out with a database schema, try it out in code and populate simple array's, something like this:

 

namespace Forum;

class Topic implements Countable, IteratorAggregate {
    private $posts = array();
    
    public function __construct(Post $firstPost) {
        $this->addPost($firstPost);
    }
    
    public function addPost(Post $post) { $this->posts[] = $post; }
    
    public function receivedReplies() { return ($this->count() - 1) > 1; }
    public function count() { return count($this->posts); }
    public function getIterator() { return new ArrayIterator($this->posts); }
    
    public function lastReceivedReplyAt() {
        if (!$this->receivedReplies()) {
            return null;
        }
        
        $date = end($this->posts)->getPostDate();
        reset($this->posts);
        return $date;
    }
}

class Post {
    private /** @var User */ $author;
    private /** @var string */ $subject;
    private /** @var string */ $text;
    private /** @var \DateTime */ $postDate;
    private /** @var \DateTime */ $modifyDate;
    
    public function __construct($author, $subject, $text, $postDate = null) {
        $this->author = $author;
        $this->subject = $subject;
        $this->text = $text;
        $this->postDate = $postDate ?: new DateTime;
    }
}

class User {
    private $alias;
}
Edited by ignace
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.