Home arrow MySQL arrow Page 2 - Building a Threaded News System With PHP

Building a Threaded News System With PHP

Ever wanted to build a threaded news system? If so, this article is for you. In it, the Free2Code team show us how to do just that using PHP and MySQL...

Author Info:
By: Free2Code Team
Rating: 5 stars5 stars5 stars5 stars5 stars / 32
December 29, 2002
  1. · Building a Threaded News System With PHP
  2. · Creating the Tables
  3. · Adding Comments (comment.php)
  4. · Conclusion

print this article

Building a Threaded News System With PHP - Creating the Tables
(Page 2 of 4 )

Firstly, the news table:

id INTEGER (11) auto_increment,
author VARCHAR (40),
title VARCHAR (40),
announcement TEXT,

Now the comments table:

id INTEGER (11) auto_increment,
news_id INTEGER (11),
rel_id INTEGER (11),
replies INTEGER (11),
author VARCHAR (40),
title VARCHAR (40),
comment TEXT,

The news table is pretty simple. We just store a unique ID for each post along with the name of the person who posted the announcement, the title of the announcement and the announcement itself.

The comments table stores three extra values -- the news_idb, the rel_id and the replies:
  • news_id: We'll use this value to show which news post a comment belongs to. For example, if a news post has the ID of 1, then any comments with a news_id of 1 will be associated with the news post ID that matches its news_id value.
  • rel_id: We will use this column to show the parent comment that a “child comment” (remember we’re working with threads) belongs to. For example, if a comment is intended to be a reply to a comment with the ID of 3, then the new comment created will have a rel_id of 3.
  • replies: We will use this to track how many replies a comment has had.
Here's the database connection script:

ini_set('register_globals', 0);
ini_set('magic_quotes_gpc', 0);

require_once 'DB.php';
//require the PEAR::DB classes.

$db_engine = 'mysql'; // database engine
$db_user = 'username'; // your username
$db_pass = 'password'; // your password
$db_host = 'localhost'; // db server
$db_name = 'database'; // select database

// build data source
$datasource = $db_engine.'://'.

// connect to database
$db = DB::connect($datasource);

// if errors die and display error
if(DB::isError($db_object)) {

// fetch items from the db as objects

Adding News Announcements (add.php)
Now we need to create the script that will allow news announcements to be made:


// we need database operations
<title>add news</title>
<h2>Add A News Accouncement</h2>

// if form has been submitted
if(isset($_POST['submit'])) {
// check they filled everything in.
if(!$_POST['author'] | !$_POST['title'] | !$_POST['announcement']) {
die('Please fill in all the fields.');

// get rid of HTML or PHP tags
$_POST['author'] = htmlspecialchars($_POST['author']);
$_POST['title'] = htmlspecialchars($_POST['title']);
$_POST['announcement'] = htmlspecialchars($_POST['announcement']);

// convert text new lines to HTML new lines.
$_POST['announcement'] = str_replace("\
", '<br>', $_POST['announcement']);

// insert into database
$insert = $db->query("INSERT INTO news (author, title, announcement)
VALUES ('".addslashes($_POST['author'])."',
echo '<p>Cheers '.$_POST['author'].', your announcement has been added, click
<a href="/index.php">here</a> to view it.</p>';

// if form hasnt been submitted
else {
// echo it out.
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
echo 'Name: <input type="text" name="author" maxlength="40"><br>';
echo 'Title: <input type="text" name="title" maxlength="40"><br>';
echo '<textarea name="announcement" rows="20" cols="60"></textarea><br>';
echo '<input type="submit" name="submit" value="Post it"><br>';
echo '</form>';

Displaying News Announcements (index.php)
Now we write the script that will list the news announcements:

<title>(Your Site) News</title>
<h2>Latest Updates and News</h2>

// get news announcements and order them in reverse (newest at the top)
$news = $db->query('SELECT * FROM news ORDER BY id DESC');

// if error print out debugging info.
if(DB::IsError($news)) {

// how many news posts?
$num = $news->numRows();

// if more than zero....
if($num > 0) {
// go thru them
while($n = $news->fetchRow()) {
$comments = $db->query("SELECT id FROM comments
WHERE news_id = '".$n->id."'");
if(DB::IsError($comments)) {
$num_comments = $comments->numRows();
// and echo them out
echo '<h4>'.stripslashes($n->title).' by '.stripslashes($n->author).'
echo '<p>'.stripslashes($n->announcement).'</p>';
echo '<p><a href="/comments.php?id='.$n->id.'&
else { // if none
// echo out so.
echo '<p>No news announcements yet.</p>';

blog comments powered by Disqus

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 – More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials