phpLens
 home  products examples manual  faq support forum  contact news  login store

Run   Overview   init.php   forums.php   topics.php   msgs.php   topics.tpl   msgs.tpl   inputmsgs.tpl   msgsfilter.tpl  

This is the most advanced phpLens example on the Web site, and some study is required before you can understand how the forum software works.

The data is stored in two tables. The table forums holds the list of forums. The table replies contains all topics and responses to the topics.

create table forums (
    forumid int auto_increment not null,
    forumname char(64) not null,
    primary key(forumname),
    key (id)
);
create table replies (
    replyid int auto_increment not null,
    subject char(80) not null,
    created datetime not null,
    modified datetime not null,
    name  char(32),
    email char(48),
    body text,
    forumkey  int,
    topickey int,
    nreplies int,
    primary key(replyid),
    key (topickey),
    key (forumkey),
    key (modified)
);

A topic is identified by topickey == -1. A response to a topic will have a positive topickey that matches the replyid of the topic. So if a topic has a replyid of 5 and topickey of -1, then all responses to that topic will have a topickey of 5.

The forumkey is a foreign key to the forums table. And nreplies is the number of responses to a particular topic.

  • init.php holds initialization and library code.
  • forums.php displays a list of forums.
  • topic.php shows the list of current topics and handles the creation of new topics. Note how the defaultLens is used to initialize topickey=1 and the creation and modification times.
  • msgs.php handles responses to topics. Note how SQL events trigger the UpdateTopic() function, which increments the nreplies field and modification date of the topic record so the newest topic update will float the topic to the top of the topic list.
  • header.php holds the html headers and the code that handles View Source that you are reading now.

Note that administrators can edit and delete records if the session variable $gAdmin is set to true. To prevent spammers from trawling for email addresses, we change @ to # using a powerLens.

If $gAdminMail is set, then all messages are also forwarded to that email address. Two constants can be set also. If ALLOWHTML is true, html is allowed in the forum, otherwise ubb codes are used [b][/b] and [i][/i]. If NOSPAM is true, the email address is mangled by changing @ to #.

Four template files are used. Topics.tpl and msgs.tpl for controlling the look and feel of the software. And inputmsgs.tpl is the template for formating replies. We embed the gTopicID in inputmsgs.tpl so that if the session expires, the gTopicID is refreshed when the user posts the reply.

The search screen results allow you to view a thread, using the special msgsfilter.tpl. We create a new $id for every new thread because we don't want any thread view to interfere with search screen.

The advantage of this forum software is that it is much shorter than equivalent PHP forums, so it much easier (but still not easy -- hey software is complex) to add functionality specific to your needs.

email: contact#phplens.com (change # to @)     telephone (malaysia): 60-3-7947 2888     fax (malaysia): 60-3-7947 2800