QUOTE
Features:
Admin Panel
- Add News
- Edit News
- Delete News
Comments
- Add Comments
- Delete Comments
- Edit Comments
BBCode
- Bold Tags- Italics Tags
- Underlined Tags
- Strike Through Tags
- Link Tags
- Image Tags
- Code Tags
- E-Mail Tags
Admin Panel
- Add News
- Edit News
- Delete News
Comments
- Add Comments
- Delete Comments
- Edit Comments
BBCode
- Bold Tags- Italics Tags
- Underlined Tags
- Strike Through Tags
- Link Tags
- Image Tags
- Code Tags
- E-Mail Tags
To start off we need to create our mysql for our news cms.
CODE
CREATE TABLE `news` (
`id` int(10) NOT NULL auto_increment,
`title` varchar(50) NOT NULL default '',
`author` varchar(30) NOT NULL default '',
`content` text NOT NULL,
`postdate` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
CREATE TABLE `news_comments` (
`id` int(10) NOT NULL auto_increment,
`author` varchar(30) NOT NULL default '',
`content` text NOT NULL,
`postdate` varchar(100) NOT NULL default '',
`nid` varchar(30) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
`id` int(10) NOT NULL auto_increment,
`title` varchar(50) NOT NULL default '',
`author` varchar(30) NOT NULL default '',
`content` text NOT NULL,
`postdate` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
CREATE TABLE `news_comments` (
`id` int(10) NOT NULL auto_increment,
`author` varchar(30) NOT NULL default '',
`content` text NOT NULL,
`postdate` varchar(100) NOT NULL default '',
`nid` varchar(30) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
This will create 2 tables, 1 table for news and one for news comments! The next part of the tutorial will show you have to create the BBCode file, if you wish to learn how to make your own read my tutorial here!
bbcode.php
CODE
<?
function bbcode($content){ // our nice bbcode function
$content = nl2br(htmlspecialchars($content)); // our message that we want to put the bbcode in
$bbcode = array( // The BBCode tags
"'[b](.*?)[/b]'", // Bold Tag [b]Bold[/b]
"'[i](.*?)[/i]'", // Italics Tag [i]Italics[/i]
"'[u](.*?)[/u]'", // Underline Tag [u]Underlined[/u]
"'[strike](.*?)[/strike]'", // Stroked Out Tag [strike]Strike[/strike]
"'[img](.*?)[/img]'", // Image Tag
"'[url=http://(.*?)](.*?)[/url]'", // Url Tag [url=http://yoursite.com]A website =D[/url]
"'[url=http://(.*?)](.*?)[/url]'", // Another Url Tag [url]yoursite.com[url]
);
$html = array( // The HTML counter part of the tags
"<strong>1</strong>", // Bold
"<em>1</em>", // Italics
"<u>1</u>", // Underlined
"<strike>1</strike>", // Stroked out text
"<a href='1' target='_BLANK'>2</a>", // Url 1 opens in a new window
"<a href='1' target='_BLANK'>1</a>", // Url 2 opens in a new window
"<img border='0' src='1'>", // Image
);
$content = preg_replace($bbcode, $html, $content); // replaces all BBCode tags with their HTML counter parts
return nl2br($content);
}
?>
function bbcode($content){ // our nice bbcode function
$content = nl2br(htmlspecialchars($content)); // our message that we want to put the bbcode in
$bbcode = array( // The BBCode tags
"'[b](.*?)[/b]'", // Bold Tag [b]Bold[/b]
"'[i](.*?)[/i]'", // Italics Tag [i]Italics[/i]
"'[u](.*?)[/u]'", // Underline Tag [u]Underlined[/u]
"'[strike](.*?)[/strike]'", // Stroked Out Tag [strike]Strike[/strike]
"'[img](.*?)[/img]'", // Image Tag
"'[url=http://(.*?)](.*?)[/url]'", // Url Tag [url=http://yoursite.com]A website =D[/url]
"'[url=http://(.*?)](.*?)[/url]'", // Another Url Tag [url]yoursite.com[url]
);
$html = array( // The HTML counter part of the tags
"<strong>1</strong>", // Bold
"<em>1</em>", // Italics
"<u>1</u>", // Underlined
"<strike>1</strike>", // Stroked out text
"<a href='1' target='_BLANK'>2</a>", // Url 1 opens in a new window
"<a href='1' target='_BLANK'>1</a>", // Url 2 opens in a new window
"<img border='0' src='1'>", // Image
);
$content = preg_replace($bbcode, $html, $content); // replaces all BBCode tags with their HTML counter parts
return nl2br($content);
}
?>
Save the code above as bbcode.php and remember to read the comments so you can learn!
Ok so now we need a page to display our news, read the comments so you know what?s going on!
news.php
CODE
<?php
ob_start(); // allows us to use cookies
include("config.php"); // includes the config file
include("bbcode.php"); // includes our bbcode file
$q = mysql_query("SELECT * FROM news ORDER BY id DESC"); // querys the database
if (mysql_num_rows($q) == "0") { // if there is nothing than we echo an error
echo ("There is no news in the database!"); // opps nothing
}
while($r=mysql_fetch_array($q)){ // fetches array
$id = $r['id']; // news id
$title = $r['title']; // news title
$author = $r['author']; // news author
$postdate = $r['postdate']; // news date
$content = bbcode($r['content']); // news content
echo ("<tr> // displays our news
<td><a href='news_comments.php?view=news&id=$id'>$title</a> Posted by <a href='/kurt/members.php?user=$author'>$author</a> At $postdate<br></td>
</tr>
<tr>
<td>$content<br>[<a href='news_comments.php?view=addcomment&id=$id'>Add a comment</a>] [<a href='news_comments.php?view=news&id=$id'>View comments</a>]<br><br></td>
</tr>");
}
if($logged[username] && $logged[level] ==5) echo ("[<a href='news_admin.php'>Administrative Panel</a>]"); // if the user is an admin display a link to the admin panel
if(!$logged[username]) echo ("[<a href='login.php'>Login</a>] [<a href='register.php'>Register</a>]"); // if the user is a guest display links to login or register
?>
ob_start(); // allows us to use cookies
include("config.php"); // includes the config file
include("bbcode.php"); // includes our bbcode file
$q = mysql_query("SELECT * FROM news ORDER BY id DESC"); // querys the database
if (mysql_num_rows($q) == "0") { // if there is nothing than we echo an error
echo ("There is no news in the database!"); // opps nothing
}
while($r=mysql_fetch_array($q)){ // fetches array
$id = $r['id']; // news id
$title = $r['title']; // news title
$author = $r['author']; // news author
$postdate = $r['postdate']; // news date
$content = bbcode($r['content']); // news content
echo ("<tr> // displays our news
<td><a href='news_comments.php?view=news&id=$id'>$title</a> Posted by <a href='/kurt/members.php?user=$author'>$author</a> At $postdate<br></td>
</tr>
<tr>
<td>$content<br>[<a href='news_comments.php?view=addcomment&id=$id'>Add a comment</a>] [<a href='news_comments.php?view=news&id=$id'>View comments</a>]<br><br></td>
</tr>");
}
if($logged[username] && $logged[level] ==5) echo ("[<a href='news_admin.php'>Administrative Panel</a>]"); // if the user is an admin display a link to the admin panel
if(!$logged[username]) echo ("[<a href='login.php'>Login</a>] [<a href='register.php'>Register</a>]"); // if the user is a guest display links to login or register
?>
Save the code above as news.php, and remember to read the comments so you can learn.
Now we are going to make a file to display the news comments, remember to read the comments!
news_comments.php
CODE
<?php
ob_start();
include("config.php");
if ($logged['username']){
switch($_GET['view']){
case 'news':
$id = $_GET['id'];
$select = "select * from news where id=$id";
$select2 = "select * from news_comments where nid=$id ORDER BY id DESC";
$getnews = mysql_query($select)
or die(mysql_error());
$getcomments = mysql_query($select2);
if (mysql_num_rows($getnews) == "0") {
echo 'Unable to find the article in our database';
exit();
}
$row = mysql_fetch_array($getnews)
or die(mysql_error());
$nid = $row['id'];
$ntitle = $row['title'];
$ncontent = $row['content'];
$nauthor = $row['author'];
$postdate = $row['postdate'];
echo ("<tr>
<td>$ntitle Posted by <a href='members.php?user=$nauthor'>$nauthor</a> At $postdate<br></td>
</tr>
<tr>
<td>$ncontent<br><br>Comments:<br></td>
</tr>");
if (mysql_num_rows($getcomments) == "0") {
echo("There are no comments!<br>[<a href='news_comments.php?view=addcomment&id=$id'>Add a comment</a>]");
}
if($logged[username] && $logged[level] ==5)
while($rowc= mysql_fetch_array($getcomments)){
$cauthor = $rowc['author'];
$ccomment = $rowc['content'];
$cdate = $rowc['postdate'];
$cid = $rowc['id'];
echo ("<tr>
<td>Comment Posted by [<a href="news_admin.php?view=editcomment&id=$cid">Edit</a>] [<a href="news_admin.php?view=deletecomment&id=$cid">Delete</a>]<br></td>
</tr>
<tr>
<td>$ccomment<br><br></td>
</tr>");
}
while($rowc= mysql_fetch_array($getcomments)){
$cauthor = $rowc['author'];
$ccomment = $rowc['content'];
$cdate = $rowc['postdate'];
echo ("<tr>
<td>Comment Posted by <a href='members.php?user=$nauthor'>$cauthor</a> At $cdate<br></td>
</tr>
<tr>
<td>$ccomment<br><br></td>
</tr>");
}
echo("[<a href='news_comments.php?view=addcomment&id=$id'>Add a comment</a>]");
break;
case 'addcomment':
ob_start();
include("config.php");
$id = $_GET['id'];
if(isset($_POST['add_comment'])) {
$author = $logged['username'];
$postdate = date('g:i A, l F j');
$comment = $_POST['comment'];
$nid = $_GET['id'];
$sql = "INSERT INTO news_comments ( `author` , `postdate`, `content`, `nid`) VALUES ('$author', '$postdate', '$comment', '$nid')";
$addblog = mysql_query($sql)
or die(mysql_error());
header("Location: news.php");
}
else
{
echo ("<form method='post' name='addcomment'>
<tr>
<td height='20'>Comment:</td>
</tr>
<tr>
<td><br><textarea rows='5' cols='35' name='comment'>Type your comment here!</textarea><br> <input type='submit' name='add_comment' value='Submit'></td>
</tr>
</form>");
}
}
if (!isset($_GET['id']))
header("Location:news.php");
}else{
exit('Hey! you need to [<a href='http://www.utsagamingservers.com/kurt/cpanel?view=login'>login</a>] first!');
}
?>
ob_start();
include("config.php");
if ($logged['username']){
switch($_GET['view']){
case 'news':
$id = $_GET['id'];
$select = "select * from news where id=$id";
$select2 = "select * from news_comments where nid=$id ORDER BY id DESC";
$getnews = mysql_query($select)
or die(mysql_error());
$getcomments = mysql_query($select2);
if (mysql_num_rows($getnews) == "0") {
echo 'Unable to find the article in our database';
exit();
}
$row = mysql_fetch_array($getnews)
or die(mysql_error());
$nid = $row['id'];
$ntitle = $row['title'];
$ncontent = $row['content'];
$nauthor = $row['author'];
$postdate = $row['postdate'];
echo ("<tr>
<td>$ntitle Posted by <a href='members.php?user=$nauthor'>$nauthor</a> At $postdate<br></td>
</tr>
<tr>
<td>$ncontent<br><br>Comments:<br></td>
</tr>");
if (mysql_num_rows($getcomments) == "0") {
echo("There are no comments!<br>[<a href='news_comments.php?view=addcomment&id=$id'>Add a comment</a>]");
}
if($logged[username] && $logged[level] ==5)
while($rowc= mysql_fetch_array($getcomments)){
$cauthor = $rowc['author'];
$ccomment = $rowc['content'];
$cdate = $rowc['postdate'];
$cid = $rowc['id'];
echo ("<tr>
<td>Comment Posted by [<a href="news_admin.php?view=editcomment&id=$cid">Edit</a>] [<a href="news_admin.php?view=deletecomment&id=$cid">Delete</a>]<br></td>
</tr>
<tr>
<td>$ccomment<br><br></td>
</tr>");
}
while($rowc= mysql_fetch_array($getcomments)){
$cauthor = $rowc['author'];
$ccomment = $rowc['content'];
$cdate = $rowc['postdate'];
echo ("<tr>
<td>Comment Posted by <a href='members.php?user=$nauthor'>$cauthor</a> At $cdate<br></td>
</tr>
<tr>
<td>$ccomment<br><br></td>
</tr>");
}
echo("[<a href='news_comments.php?view=addcomment&id=$id'>Add a comment</a>]");
break;
case 'addcomment':
ob_start();
include("config.php");
$id = $_GET['id'];
if(isset($_POST['add_comment'])) {
$author = $logged['username'];
$postdate = date('g:i A, l F j');
$comment = $_POST['comment'];
$nid = $_GET['id'];
$sql = "INSERT INTO news_comments ( `author` , `postdate`, `content`, `nid`) VALUES ('$author', '$postdate', '$comment', '$nid')";
$addblog = mysql_query($sql)
or die(mysql_error());
header("Location: news.php");
}
else
{
echo ("<form method='post' name='addcomment'>
<tr>
<td height='20'>Comment:</td>
</tr>
<tr>
<td><br><textarea rows='5' cols='35' name='comment'>Type your comment here!</textarea><br> <input type='submit' name='add_comment' value='Submit'></td>
</tr>
</form>");
}
}
if (!isset($_GET['id']))
header("Location:news.php");
}else{
exit('Hey! you need to [<a href='http://www.utsagamingservers.com/kurt/cpanel?view=login'>login</a>] first!');
}
?>
Save the above code as news_comments.php and read the comments!
Now for the admin panel!
news_admin.php
CODE
<?
ob_start();
include('config.php');
if ($logged['level'] == '5') {
switch($_GET['view']) {
case "addnews" :
include("config.php");
if(isset($_POST['add_news'])) {
$title=$_POST['title'];
$author=$logged['username'];
$postdate=date("g:i A, l F j");
$content=$_POST['content'];
$sql = "INSERT INTO news (title, author, postdate, content) VALUES ('$title', '$author', '$postdate', '$content')";
$addnews = mysql_query($sql)
or die(mysql_error());
header("Location:news.php");
}
else
{
echo ("
<form method='post' name='addnews'>
<tr>
<td height='20'>Article Title:<br></td>
</tr>
<tr>
<td> <input class='content_box' Type='text' name='title' value='Article's Title'></td>
</tr>
<tr>
<td height='20'><br>News Article:</td>
</tr>
<tr>
<td><br><textarea rows='15' cols='95' name='content'>Type your article here!</textarea><br> <input type='submit' name='add_news' value='Submit'></td>
</tr>
</form>");
}
break;
case "editnews" :
include("config.php");
$id = $_GET['id'];
if(!$_GET['id'])
{
header("Location:news_admin.php");
}
$select = mysql_query("select * from news where id=$id");
if (mysql_num_rows($select) == "0") {
echo 'Unable to find the news article in the database!';
exit();
}
if(isset($_POST['edit_news'])) {
$title=$_POST['title'];
$content=$_POST['content'];
$sql = mysql_query("update news set title = '$title', content = '$content' where id = '$id'");
echo ("<meta http-equiv='Refresh' content='1; URL=news.php'/>Your article has been updated! You will now be redirected");
exit;
}
else
{
$get = mysql_query("select * from news where id=$id");
$get = mysql_fetch_array($get);
echo ("
<form method='post' name='editnews'>
<tr>
<td height='20'>Article Title:<br></td>
</tr>
<tr>
<td> <input Type='text' name='title' value='$get[title]'></td>
</tr>
<tr>
<td height='20'><br>News Article:</td>
</tr>
<tr>
<td><br><textarea rows='15' cols='95' name='content'>$get[content]</textarea><br> <input type='submit' name='edit_news' value='Submit'></td>
</tr>
</form>");
}
break;
case "deletenews" :
include("config.php");
$id = $_GET['id'];
if(!$_GET['id'])
{
header("Location:news_admin.php");
}
$select = mysql_query("select * from news where id=$id");
if (mysql_num_rows($select) == "0") {
echo 'Unable to find the news article in the database!';
exit();
}
$delete = mysql_query("DELETE FROM news WHERE id = '$id'") or die(mysql_error());
$delete2 = mysql_query("DELETE FROM news_comments WHERE nid = '$id'") or die(mysql_error());
echo("<meta http-equiv='Refresh' content='1; URL=news.php'/>Your article has been deleted! You will now be redirected");
break;
case "deletecommet" :
include("config.php");
$id = $_GET['id'];
if(!$_GET['id'])
{
header("Location:news_admin.php");
}
$select = mysql_query("select * from news_comments where id=$id");
if (mysql_num_rows($select) == "0") {
echo 'Unable to find the comment in the database!';
exit();
}
$delete = mysql_query("DELETE FROM `news_comments` WHERE id = '$id'") or die(mysql_error());
echo("<meta http-equiv='Refresh' content='1; URL=news.php'/>The comment has been deleted! You will now be redirected");
break;
case "editcomment" :
include("config.php");
$id = $_GET['id'];
if(!$_GET['id'])
{
header("Location:news_admin.php");
}
$select = mysql_query("select * from news_comments where id=$id");
if (mysql_num_rows($select) == "0") {
echo 'Unable to find the comment in the database!';
exit();
}
if(isset($_POST['edit_comment'])) {
$comment=$_POST['comment'];
$sql = mysql_query("update news_comments set content = '$content' where id = '$id'");
echo ("<meta http-equiv='Refresh' content='1; URL=news.php'/>The comment has been updated! You will now be redirected");
exit;
}
else
{
$get = mysql_query("select * from news_comments where id=$id");
$get = mysql_fetch_array($get);
echo ("
<form method='post' name='editcomment'>
<tr>
<td height='20'>Comment:</td>
</tr>
<tr>
<td><br><textarea rows='15' cols='95' name='comment'>$get[content]</textarea><br> <input type='submit' name='edit_comment' value='Submit'></td>
</tr>
</form>");
}
break;
default:
echo "Welcome to your news admin area $logged[username]! [<a href='news_admin.php?view=addnews'>Add News</a>]<br><br>";
$q = mysql_query("SELECT * FROM news ORDER BY id DESC");
if (mysql_num_rows($q) == "0") {
echo ("There is no news in the database! [<a href='news_admin.php?view=addnews'>Add News</a>]");
}
while($r=mysql_fetch_array($q)){
$id = $r['id'];
$title = $r['title'];
$author = $r['author'];
$postdate = $r['postdate'];
echo ("<tr>
<td><a href='news_comments.php?view=news&id=$id'>$title</a> Posted by <a href='members.php?user=$author'>$author</a> At $postdate [<a href='news_admin.php?view=editnews&id=$id'>Edit</a>] [<a href='news_admin.php?view=deletenews&id=$id'>Delete</a>]<br></td>
</tr>");
}
break;
}
}else{
exit('Hey! where do you think your going?');
}
?>
ob_start();
include('config.php');
if ($logged['level'] == '5') {
switch($_GET['view']) {
case "addnews" :
include("config.php");
if(isset($_POST['add_news'])) {
$title=$_POST['title'];
$author=$logged['username'];
$postdate=date("g:i A, l F j");
$content=$_POST['content'];
$sql = "INSERT INTO news (title, author, postdate, content) VALUES ('$title', '$author', '$postdate', '$content')";
$addnews = mysql_query($sql)
or die(mysql_error());
header("Location:news.php");
}
else
{
echo ("
<form method='post' name='addnews'>
<tr>
<td height='20'>Article Title:<br></td>
</tr>
<tr>
<td> <input class='content_box' Type='text' name='title' value='Article's Title'></td>
</tr>
<tr>
<td height='20'><br>News Article:</td>
</tr>
<tr>
<td><br><textarea rows='15' cols='95' name='content'>Type your article here!</textarea><br> <input type='submit' name='add_news' value='Submit'></td>
</tr>
</form>");
}
break;
case "editnews" :
include("config.php");
$id = $_GET['id'];
if(!$_GET['id'])
{
header("Location:news_admin.php");
}
$select = mysql_query("select * from news where id=$id");
if (mysql_num_rows($select) == "0") {
echo 'Unable to find the news article in the database!';
exit();
}
if(isset($_POST['edit_news'])) {
$title=$_POST['title'];
$content=$_POST['content'];
$sql = mysql_query("update news set title = '$title', content = '$content' where id = '$id'");
echo ("<meta http-equiv='Refresh' content='1; URL=news.php'/>Your article has been updated! You will now be redirected");
exit;
}
else
{
$get = mysql_query("select * from news where id=$id");
$get = mysql_fetch_array($get);
echo ("
<form method='post' name='editnews'>
<tr>
<td height='20'>Article Title:<br></td>
</tr>
<tr>
<td> <input Type='text' name='title' value='$get[title]'></td>
</tr>
<tr>
<td height='20'><br>News Article:</td>
</tr>
<tr>
<td><br><textarea rows='15' cols='95' name='content'>$get[content]</textarea><br> <input type='submit' name='edit_news' value='Submit'></td>
</tr>
</form>");
}
break;
case "deletenews" :
include("config.php");
$id = $_GET['id'];
if(!$_GET['id'])
{
header("Location:news_admin.php");
}
$select = mysql_query("select * from news where id=$id");
if (mysql_num_rows($select) == "0") {
echo 'Unable to find the news article in the database!';
exit();
}
$delete = mysql_query("DELETE FROM news WHERE id = '$id'") or die(mysql_error());
$delete2 = mysql_query("DELETE FROM news_comments WHERE nid = '$id'") or die(mysql_error());
echo("<meta http-equiv='Refresh' content='1; URL=news.php'/>Your article has been deleted! You will now be redirected");
break;
case "deletecommet" :
include("config.php");
$id = $_GET['id'];
if(!$_GET['id'])
{
header("Location:news_admin.php");
}
$select = mysql_query("select * from news_comments where id=$id");
if (mysql_num_rows($select) == "0") {
echo 'Unable to find the comment in the database!';
exit();
}
$delete = mysql_query("DELETE FROM `news_comments` WHERE id = '$id'") or die(mysql_error());
echo("<meta http-equiv='Refresh' content='1; URL=news.php'/>The comment has been deleted! You will now be redirected");
break;
case "editcomment" :
include("config.php");
$id = $_GET['id'];
if(!$_GET['id'])
{
header("Location:news_admin.php");
}
$select = mysql_query("select * from news_comments where id=$id");
if (mysql_num_rows($select) == "0") {
echo 'Unable to find the comment in the database!';
exit();
}
if(isset($_POST['edit_comment'])) {
$comment=$_POST['comment'];
$sql = mysql_query("update news_comments set content = '$content' where id = '$id'");
echo ("<meta http-equiv='Refresh' content='1; URL=news.php'/>The comment has been updated! You will now be redirected");
exit;
}
else
{
$get = mysql_query("select * from news_comments where id=$id");
$get = mysql_fetch_array($get);
echo ("
<form method='post' name='editcomment'>
<tr>
<td height='20'>Comment:</td>
</tr>
<tr>
<td><br><textarea rows='15' cols='95' name='comment'>$get[content]</textarea><br> <input type='submit' name='edit_comment' value='Submit'></td>
</tr>
</form>");
}
break;
default:
echo "Welcome to your news admin area $logged[username]! [<a href='news_admin.php?view=addnews'>Add News</a>]<br><br>";
$q = mysql_query("SELECT * FROM news ORDER BY id DESC");
if (mysql_num_rows($q) == "0") {
echo ("There is no news in the database! [<a href='news_admin.php?view=addnews'>Add News</a>]");
}
while($r=mysql_fetch_array($q)){
$id = $r['id'];
$title = $r['title'];
$author = $r['author'];
$postdate = $r['postdate'];
echo ("<tr>
<td><a href='news_comments.php?view=news&id=$id'>$title</a> Posted by <a href='members.php?user=$author'>$author</a> At $postdate [<a href='news_admin.php?view=editnews&id=$id'>Edit</a>] [<a href='news_admin.php?view=deletenews&id=$id'>Delete</a>]<br></td>
</tr>");
}
break;
}
}else{
exit('Hey! where do you think your going?');
}
?>
Save the above code as news_admin.php and read the comments!
And that's all folks!
Enjoy!

