What you will learn:
You will learn how to make a News system for your website that allows you to add, edit and delete news from a database. You will also learn how to make a
"comments" system with it, so users can comment on it. You will be able to add, edit, and delete comments as well.
Who should be using this tutorial:
You should understand the basics of SQL and PHP and know how to use MySQL or PhpMyAdmin before using this...
=======================
Alright down to business...This is a long script. Please don't post until I have stated that it is finished...Thanks.
All you need for this is two pages. It all deals with "actions" or index.php?action=blah and a security page.
Before you start, you need to create a database. If you haven't already. Then you need to put in these 2 scripts:
1.
CREATE TABLE `news` (
`id` int(10) unsigned NOT NULL auto_increment,
`postdate` timestamp NULL default NULL,
`title` varchar(50) NOT NULL default '',
`newstext` text NOT NULL,
PRIMARY KEY (`id`),
KEY `postdate` (`postdate`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
That will be your news table. Now time for the comments that will be added onto the news...
2.
CREATE TABLE `news_comments` (
`id` int(10) unsigned NOT NULL auto_increment,
`news_id` int(10) unsigned NOT NULL default '0',
`name` varchar(40) NOT NULL default '',
`comment` text NOT NULL,
PRIMARY KEY (`id`),
KEY `news_id` (`news_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;
And that will sync the news comments with the news posts.
First you need to create index.php in the directory news ( /news/index.php )
Here's what's going to happen. I'll tell you about the sessions after we finish index.php:
<?php session_start(); ?>
<?php include ('../header.php');
$max_items = 10;
// This will tell how many news items will be displayed. Just the link will be displayed though...
// The whole news part will be displayed on individual pages.
$db = mysql_connect ('localhost','USERNAME','PASSWORD');
mysql_select_db ('DATABASE_NAME',$db);
// Replace USERNAME, PASSWORD, and DATABASE NAME with your information from your database.
// Now we are going to make all of these functions, so you can just easily call them into a switch at the end....oh how nice ;)
function displayNews($all) {
global $db, $max_items;
// This is just making the variables usable throughout the whole script...
if ($all == 0) {
$query = "SELECT id, title, newstext," .
"DATE_FORMAT(postdate, '%b %e, %Y') as date " .
"FROM news ORDER BY id DESC LIMIT $max_items";
// A simple if statement, saying that if $all == 0 ($all being the number of items shown. If $all is 0, then it will limit the query to only showing the
latest 10 news posts. If $all is more than 10, then all of the data in the database will be shown.
} else {
// Else show all of the data. In descending order, so the latest news will be shown first.
$query = "SELECT id, title, newstext," .
"DATE_FORMAT(postdate, '%b %e, %Y') as date " .
"FROM news ORDER BY id DESC";
}
$result = mysql_query ($query);
while ($row = mysql_fetch_assoc ($result)) {
// Next we are going to create a while loop, that gathers all of the information, and orders it into a nice little table.
echo "<TABLE border=\"0\" width=\"400\" align=\"center\">\n";
// We can now select the rows, because we stated $row = mysql_fetch_assoc($result). Because of this, each row is accessable as an array, so they can
be added as variables. You could just use $row['blah'] for the whole thing, but I like to make it simpler, and put them into separate variables...
$date = $row['date'];
$title = htmlentities ($row['title']);
$news = strip_tags ($row['newstext'], '<a><b><i><u>');
// We are making sure no tags but the ones stated above get added to the news script. Feel free to add more, like <font> and what not...
$news = nl2br ($news);
// this just creates a NEW LINE (nl) to (2) a LINE BREAK (br)
$id = $row['id'];
echo "<TR><TD><p><a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id=$id\"<b>$title</b></a> posted on
<i>$date</i></p></TD>\n";
echo "\n";
$comment_query = "SELECT count(*) FROM news_comments " .
"WHERE news_id={$row['id']}";
$comment_result = mysql_query ($comment_query);
$comment_row = mysql_fetch_row($comment_result);
// This just gets all of the information from the comments table...
echo "<TD algin=\"right\"><p><a href=\"{$_SERVER['PHP_SELF']}" .
"?action=showcom&id={$row['id']}\">Comment
s</a> " . "{" . "$comment_row[0]}</p></TD></TR>\n";
// This echos the link "Comments" (you'll see that you can either choose the title of the news post, to show no comments, or you can click the
comments link next to the title, to show the comments) with how many comments are actually posted on that script.
echo "</TABLE>\n";
echo "\n";
}
if ($all == 0) {
echo "<br /><center><a href=\"{$_SERVER['PHP_SELF']}" .
"?action=all\">View all news</a></center>\n";
// If all of the news = 0, then add the link "View all news" so you can view the complete list of news instead of just the $max_items you listed
above.
}
}
// This next function will display one news item, with out the comments showing. This is what function will happen when you click the title.
function displayOneItem($id) {
global $db;
<!!-- CONTINUED --!!>
THAT MEANS DON'T POST!