PDA

View Full Version : php news system [Tutorial]



ADAM:
01-02-2006, 08:35 PM
Hello,
Well this a great tutorial on how to make a php new system.
I take no credit for the tutorial becuase i got it of a forum.
This is a very basic news sytem and if any errors are found in the tutorial i will fix them :)

Warning: You host my support php and MySQL

Step 1
Create a database in your control panel

Step 2
Go into Phpmyadmin and insert this table

CREATE TABLE news (
id int(11) NOT NULL auto_increment,
title varchar(50) default NULL,
author varchar(30) default NULL,
content text,
postdate varchar(100) default NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM ;

CREATE TABLE comments (
id int(10) NOT NULL auto_increment,
nid int(10) default NULL,
author varchar(30) default NULL,
`comment` text,
PRIMARY KEY (id)
) ENGINE=MyISAM ;

This will create 2 tables, one for news and the other for comments.

Step 3
Now paste the php code into your text editor or whatever you are using

<?php
$db_name = "database"; //this is your database name
$db_user = "username"; //this is your database username for the database specified above
$db_pass = "password"; //this is your database password for the user specified above
$db_host = "localhost"; //this is your host for the database, best to leave it as localhost

mysql_connect("$db_host", "$db_user", "$db_pass")or die(mysql_error());
mysql_select_db("$db_name") or die(mysql_error());

$news_displaymax = "10"; //max news displayed on page
?>
Save that file as "config.php"
now basically in this file we have set variables, and then connected to a database, we have also set a variable to set a max amount of news display on page.

Step 4
In your EDITOR create a new file and put this into it.

<?php
include "config.php"; //included the file config.php (the one we just made)

switch($_GET['act']){ //remember from my other tutorial about switches, this is abit more advanced

case 'view':

$id = $_GET['id']; //used another $_GET[] inside a $_GET[] to it becomes ?name=var&name=var
$sql = "SELECT * FROM news WHERE id=$id"; //we make a sql to select the enws where id = $id
$sql2 = "SELECT * FROM comments where nid=$id ORDER by id ASC"; //we do the same here but for the comments where nid = $id

$getone = mysql_query($sql) //we now put the sql into a query
or die(mysql_error()); //if there is an error we display the error that mysql returns
$getcomments = mysql_query($sql2); //we do the same as above


if (mysql_num_rows($getone) == "0") { //we use an if statement to see if there is no data in the table

echo 'Unable to find the article in our database'; //we echo out a sentance to the page viewer

exit(); //we exit the script which stops page creation completely nothing under this will be made if it is executed
} //we close the if statement

$row = mysql_fetch_array($getone) //we fetch the arrays of the sql so we can display each news post
or die(mysql_error());

//I put them all in variables as i find it easier.
$nid = $row['id'];
$ntitle = $row['title'];
$ncontent = $row['content'];
$nauthor = $row['author'];
$postdate = $row['postdate'];

//I now create the design on the news display
echo ("<TABLE width=400>\n
<TR><TD><b>$ntitle<b></TD>\n</tr>
<TR><TD>Posted by $nauthor on $postdate<br>\n
-------------------------------------------------------------------------------</td></tr>\n
<tr><td>$ncontent</td></tr>\n
<tr><td><hr color=\"#820000\"></td></tr>\n
<tr><td><br><hr color=\"#820000\"></td></tr>\n
<tr><td><b>comments:</b></td></tr>");

while($rowc= mysql_fetch_array($getcomments))//i use a while statement to fetch each comment
{
//once again put the data in variables
$cauthor = $rowc['author'];
$cdata = $rowc['comment'];
$plink = "";
$pend = "";

//we echo out the comments layout
echo ("<div class=\"comments\">");
echo ("<tr><td>Comment Posted by: $plink $cauthor $pend</td></tr>\n
<tr><td>-------------------------------------------------------------------------------</tr></td>\n
<tr><td>$cdata</td></tr>\n
<tr><td>-------------------------------------------------------------------------------<br><br><br><br></td></tr>");
echo ("</div>");
}

//now we add a submit form to submit comments
echo ("<tr><td><b>Comment:</b></td></tr>\n
<tr><td><form method=\"POST\" action=\"?act=comment&nid=$nid\">\n
<textarea name=comment rows=\"7\" cols=\"30\"></textarea><br>\n
<input type=\"submit\" name=\"addcomment\" value=\"Add Comment\"></td></tr>
<tr><td><a href=\"index.php\">Go back to Index</a></td></tr>"); //link going back to the index page.

echo "</TABLE>";


break; // close the switch

case 'comment': //a new switch named comment

if (!$_POST['addcomment']) //if the user tries to access this page without submitting the form it returns an error
{
echo "Access Denied, please submit the form first";
exit();
}else{ //else if the user did submit the form, he/she carries on
$nid = $_GET['nid']; //get $nid
$commentor = "Guest"; //sets what the commenter is called

$ccontent= nl2br( strip_tags($_POST['comment'],"<b><i><u><a><font>")); //strips all tags from the comment made
//this prevents unwanted html in it.

$sql = "INSERT INTO comments (nid,author,comment) VALUES('$nid','$commentor','$ccontent')"; //makes the sql

$addedcomments = mysql_query($sql) //querys the sql inside a variable
or die(mysql_error());
echo "Thank you, your comment has been successfully added, please wait while we redirect you"; //echo out a thankyou
echo "<meta http-equiv=\"Refresh\" content=\"0; URL=?act=view&id=$nid\"/>"; //echo out a script to redirect you
}

break;

default:

$sql = "SELECT * FROM news order by id desc LIMIT $news_displaymax"; //an sql where limit is our display limit we set in our config file
$getnews = mysql_query($sql) //query the sql
or die(mysql_error());

if (mysql_num_rows($getnews) == "0") //see if any data is in the table
{
echo '<center>No news avaliable to display at this current time.</center>'; //if no data is found echo's this
}

while($news = mysql_fetch_array($getnews)){ //makes a while statement to show every news post up to the limit we chose

//put into variables again!
$nid = $news['id'];
$ntitle = $news['title'];
$ncontent = $news['content'];
$nauthor = $news['author'];
$postdate = $news['postdate'];

$csql = "SELECT * FROM comments WHERE nid=$nid"; //another sql

$count = mysql_query($csql); //query it

$cnum = mysql_num_rows($count); //count number of rows


//make the news layout

echo ("<TABLE width=400>\n
<TR><TD><hr color=\"#820000\"><br><b><a href=\"?act=view&id=$nid\">$ntitle<b></a></TD>\n</tr>
<TR><TD>Posted by $nauthor at $postdate<br>\n
-------------------------------------------------------------------------------</td></tr>\n
<tr><td>$ncontent<br>\n
-------------------------------------------------------------------------------
<align=right><a href=\"?act=view&id=$nid\">Comments($cnum)</a><br><br><hr color=\"#820000\"></td></tr>
</TABLE>\n");
}//close the while loop
break;//close the class switch
}//close the switch statement
?>
Save this file as "news.php"
basically follow the comments on the code to understand it. Overall, this is basically making you see the news, see the news with comments and add comments all within the same page.

Step 5
Now we shall create the add news page!

<?php
include "config.php"; //included the file config.php (the one we just made)

if (!$_POST['submit']) { //checks to see if the user submitted the form yet
//if not will echo out this form, this is simple html, and i will not comment this
echo (" <table>
<form method=\"post\">
<tr><td>
<strong>Title:</strong>
</td><td>
<input type=\"text\" name=\"title\" value=\"Title\">
</td></tr>
<tr><td>
<strong>Author:</strong>
</td><td>
<input type=\"text\" name=\"author\" value=\"Author\">
</td></tr>
<tr><td colspan=\"2\">
<textarea rows=14 cols=70 name=\"news\">Your News Goes Here</textarea>
</td></tr>
<tr><td colspan=\"2\">
<input type=\"submit\" name=\"submit\" Value=\"Submit\">
</td></tr>
</form></table>");
}else{ //if the user has submitted the form then it will do this instead
//sets the variables from the form
$author = $_POST['author'];
$news = $_POST['news'];
$title = $_POST['title'];
$postdate = date("g:i A, l F j"); //uses date function which is built into php

$addsql = "INSERT into news (author,content,title,postdate) VALUES('$author','$news','$title','$postdate')";
//makes the sql
$addnews = mysql_query($addsql)
or die(mysql_error());
//queries the sql
echo "Your news has been sumbitted.<br><a href=\"main.php\">main</a>"; //echo's out the result.
}
?>
Save the file as "addnews.php"
finally we are now finished. This basically lets you add news to the database which is then displayed from the database onto the page in news.php, congratulate yourself on making your own news system!


<?php
include("news.php");
?>
Put this where ever you want the news to be dislpayed.

If you have any problem please just say! And i will fix it :)

Thanks
Adam

Jamie.
01-02-2006, 08:37 PM
Very Good adam. +rep

i might use it soon

ADAM:
01-02-2006, 08:46 PM
Thanks for the comment and rep Bondie :)

- Adam
(But all credit goes to the forum i got it of, once i find the link again i will post it.)

Dan Williamson
01-02-2006, 08:48 PM
Hey,

HMMM Looks very much like on off P2L :)

Good try anyway though needs more explaining.

- Dan

ADAM:
01-02-2006, 08:50 PM
Ok, thanks for that. Ive just found the forum.

http://forums.liway.com/viewtopic.php?t=61

I should of just posted the link instead of wasting my time, but some people would say its spam.

(I find it hard to do long tutorial on habboxforum in that little box. Every time i scroll down the whole page goes down :S)

Dan Williamson
01-02-2006, 08:54 PM
Hey,

I always write my tutorials in Notepad only using Habbox's BB codes.

- Dan

Coding4Newbs
02-02-2006, 04:39 AM
Although from p2l, thanks anyway

:Blob
02-02-2006, 06:26 PM
I don't see much point in posting this, and it is in the wrong forum.

Edit:
It isn't very secure if you allow people you don't trust to submit news with that script.

And that comes from you.

And you can't post in tutorials, you have to get it


MOVED

if its good

Always
02-02-2006, 06:51 PM
Alright tutorial;) +rep for taking the time to bother making it:D

Dakara
02-02-2006, 07:24 PM
Alright tutorial;) +rep for taking the time to bother making it:D
Obviously you didn't read it.

TheEmbraces
02-02-2006, 07:35 PM
This news system is copied and also pretty insecure.

You could use some kind of encryption in the system. And also you shouldn't really copy tutorials off other sites & forums.

I suggest totally re-writing this News System so it's more secure and your own work. I suggest looking into system security and whatnot for you websites and PHP scripts.

TheEmbraces

Want to hide these adverts? Register an account for free!