Jump to content
xisto Community
alex1985

News System (cms) .::DeadMan::. Paraphrased by alex1985

Recommended Posts

A really nice tutorial how to create a full news system, a good guide to follow.

 

The features are:

 

1. Admin Panel

 

a) Add News

 

:) Edit News

 

c) Delete News

 

2. Comments

 

a) Add Comments

 

;) Delete Comments

 

c) Edit Comments

 

3. BBCode

 

a) Bold Tags/Italics Tags

 

;) Underlined Tags

 

c) Strike Through Tags

 

d) Link Tags

 

e) Image Tags

 

f) Code Tags

 

g) E-Mail Tags

 

At first, you need to create certain (2) tables which are news and comments for your system able to run:

 

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;

Secondly, you need to create a file which is called "bbcode.php", I guess, you know for what, because the name of it sounds its function.

 

<?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=(.*?)]'", // 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);}?>

Thirdly, we need to create a page that may display our news, which is "news.php":

 

<?phpob_start(); // allows us to use cookiesinclude("config.php"); // includes the config fileinclude("bbcode.php"); // includes our bbcode file$q = mysql_query("SELECT * FROM news ORDER BY id DESC"); // querys the databaseif (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 panelif(!$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?>

Fourthly, we need to create a file named "news_comments.php" in order to display comments:

 

<?phpob_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='utsagamingservers.com/kurt/cpanel?view=login&%2339; first!');}?>

Fifthly, "news_admin.php" will be used for administration purposes:

 

<?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?');}?>

Lastly, you need create the configuration file that will access your database settings ("config.php"):

 

<?php$host = "localhost";$dbuser = "username";$dbpassword = "password";$dbname = "table/database";$connection = mysql_connect($host, $dbuser, $password) or die(mysql_error());mysql_select_db($dbname) or die(mysql_error());?>

That's it. Any replies to improve it are welcomed!!!

Share this post


Link to post
Share on other sites

This is very cool, I really would love to learn PHP and CSS in full. But I don't have the time! But this tutorial is great, but I use Wordpress so I don't exactly need it, but I could make mods to Wordpress now, using some of this knowledge! Thanks, Sam.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.