OxyScripts.com
Menu spacer Home Tutorials Articles Code Forums irc.freenode.net #oxyscripts
Main (PHP)
Home Forums PHP News PHP Tutorials Articles PHP Code Snippets Contact Us Sysadmin Resources Books Template Shop
3rd Party Streams
SlashDot PHPDeveloper.org PHP.Net
Resources
PHP Manual MySQL Manual Smarty Manual PEAR Manual PHP-GTK Manual Symfony Manual
Code Snippets
Authentication Database Graphics HTTP Miscellaneous Time/Date
Affiliates
Scripts TutorialMan TutorialGuide CodingForums.com PHP Scripts Cheap Web Hosting Affordable Web Hosting

Search This Site :     PHP Function Reference :
 

News management with PHP

By Oxy (2002-04-05. 70652 views.)
This tutorial will show you how to make a simple news management script.

A lot of people have asked me how to make a simple news publishing script. Well here's the tutorial :) The script is going to comprise of 4 parts, posting, viewing, editing, and deleting. This script will be VERY basic, but easy to modify.

Database Table

Firstly we need to create our table to store the news. We'll need a row for the posts' id, so we can identify each post easily, as they all have unique numbers. We'll also need a row for the title, the news text, one for the author's name and one for the date it was posted. Lets make this table :



CREATE TABLE news (
id INT (11) not null AUTO_INCREMENT,
title VARCHAR (50),
news TEXT ,
author VARCHAR (50) ,
date DATE ,
PRIMARY KEY (id)
);


The 'auto_increment' setting for the 'id' row means that each piece of news we enter will have a unique id, by which we can identify it later.


----------------------------------------------------------------------------


Config File

Now, in order to make our script easy to customise, we need to make a config file. Edit the values as needed and save it as config.php.

<?php

/* config.php */
$db_host "localhost"
$db_user "user"
$db_pass "password"
$db_name "database"

?>



----------------------------------------------------------------------------


Post News Form

Having set the table and config up lets do some news posting ! Our news posting script will be in 2 parts, the first part a form into which we will enter the news which is to be posted. The second part will input this news into our database.

Here's the code for our form, save it as post_news.html :



<html> 
<head> 
<title>Post News</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 
<body bgcolor="#FFFFFF" text="#000000"> 
<form name="post news" method="post" action="post_news_process.php"> 
  <p>Title : <input type="text" name="title"> 
  
 
  Author : <input type="text" name="author"> 
    
 
    News : 
    
 
    <t3xtarea name="news" cols="40" rows="6"></t3xtarea></p> 
  <p><input type="submit" name="Submit" value="POST !"></p> 
  </form> 
</body> 
</html>




Notice action="post_news_process.php" , this means the data entered into the form will be taken to our second script, where it can then be processed. Also notice name="news" these name attributes will be useful later, when we have to identify what data was entered where.


----------------------------------------------------------------------------


Process The Post News Form

Now we can make our second component for news posting. In this part, we need to do the following :

Connect to the database, retrieve the data from our form (post_news.html) , then enter the data into the correct row of our news table.

Here it goes, save it as post_news_process.php :

<?php

/* post_news_process.php */
include "config.php"

$db mysql_connect($db_host$db_user$db_pass); 
mysql_select_db ($db_name) or die ("Cannot connect to database"); 
/* We have now connected, unless you got an error message */ 
/* Lets post some news ! */ 
$query "INSERT INTO news(title, news, author, date) 
VALUES('$_POST[title]','$_POST[news]','$_POST[author]', now())"

mysql_query($query); 
echo 
"News item entered."
mysql_close($db); 

?>


That should put some news in your database, if it doesn't, it's because you've entered your MySQL user/password wrong.

Lets talk about this code, firstly you'll notice this - INSERT INTO news(title, news, author, date) VALUES('$_POST[title]','$_POST[news]','$_POST[author]', now()). That's a SQL query, which tells our database what to do with the data, as you can see in our form we have fields with name="news", when this is put into our processing form it is retrieved as $_POST[news], this is then inserted into the news column of our database.

The now() function is mySQL, which enters a datestamp into our database in the form mm-dd-yy.

So thats part 1 of 3 done ! We have successfully posted ( entered ) news into the database. Let's move on !


----------------------------------------------------------------------------

Viewing News

Now we've entered our news into the database, we need to be able to view it, this is made up of two parts, firstly we must extract the news from our mySQL database, and secondly we must display this data. We have decided to only display the 10 newest news posts. Here it goes :

<?php

/* view.php */
include "config.php"

$db mysql_connect($db_host,$db_user,$db_pass); 
mysql_select_db ($db_name) or die ("Cannot connect to database"); 
/* We have now connected, unless you got an error message */ 
/* Lets extract the data (news) from the database */ 
$query "SELECT title, news, author, date FROM news ORDER BY id DESC LIMIT 10"
$result mysql_query($query); 
/* Here we fetch the result as an array */ 
while($r=mysql_fetch_array($result)) 
{   
/* This bit sets our data from each row as variables, to make it easier to display */ 
    
$title=$r["title"]; 
    
$news=$r["news"]; 
    
$author=$r["author"]; 
    
$date=$r["date"];    
/* Now lets display the news */ 
    
echo "<TABLE> 
    <TR> 
        <TD>$title</TD> 
    </TR> 
    <TR> 
        <TD>$news</TD> 
    </TR> 
    <TR> 
        <TD>Posted by $author on $date</TD> 
    </TR> 
    </TABLE>"


mysql_close($db); 

?>


Save that as view.php. The display is very simple, and should be very easy for you to modify and make look all nice and shiny. There aren't too many complicated things in the view.php code, but if you have any problems, spam the forums !


----------------------------------------------------------------------------

Change News Form

We've now successfully posted our news, and it is viewable by your visitors, well what happens if you've made a mistake in your news post ? At the moment there's nothing you can do about it, so lets write an edit news script. This script will have three parts, one selecting which news post to edit, the next is to bring up the content of the news post, and finally we need to save the edited post to the database. Save this as edit_process.php.

<?php

/* edit_process.php */
include "config.php"

$db mysql_connect($db_host,$db_user,$db_pass); 
mysql_select_db ($db_name) or die ("Cannot connect to database"); 
/* We have now connected, unless you got an error message */ 
/* We now select one news post, and bring it into some text boxes */ 
$query "SELECT title, news FROM news WHERE id = $_GET[id]"
$result mysql_query($query); 
while(
$r=mysql_fetch_array($result)) 
{   
/* This bit sets our data from each row as variables, to make it easier to display */ 
$title=$r["title"]; 
$news=$r["news"]; 
/* Now lets display the titles */ 
    
echo "<form name='edit_process.php' method='post' action='edit_save.php?id=$_GET[id]'> 
  <p>Title : 
    <input type='text' name='title' value='$title'> 
  </p> 
  <p>News :</p> 
  <p> 
    <t3xtarea name='news' cols='40' rows='6'>$news</t3xtarea> 
  </p> 
  <p> 
    <input type='submit' name='Submit' value='Save'> 
  </p> 
</form>"


mysql_close($db); 

?>


Note: Change 't3xtarea' to 'textarea' above.

----------------------------------------------------------------------------


Processing The Change News Form

We have now displayed, and edited the text, but we now need to save it to the database.

Save this as edit_save.php:

<?php

/* edit_save.php */

include "config.php"

$db mysql_connect($db_host,$db_user,$db_pass); 
mysql_select_db ($db_name) or die ("Cannot connect to database"); 
/* We have now connected, unless you got an error message */ 
/* Lets save some news ! */ 
$query "UPDATE news SET title='$_POST[title]', news='$_POST[news]' WHERE id = $_GET[id]"
$result mysql_query($query); 
echo 
"News item saved."
mysql_close($db); 

?>


Our news post is now saved into the database, now lets talk about how that worked. We used our text box to display the title and news text, so that we could edit them in our browser. We set the action for the form as action="edit_save.php?id=$_GET[id] so that we could identify which news post to save the edited news to.

The news post id was carried through to edit_save.php in the URL, which is an easy and convenient way to do this. In edit_save.php we used this query, UPDATE news SET title='$_POST[title]', news='$_POST[news]' WHERE id = $_GET[id], to save (update) the news post. It only updated the title and news fields, as this is all we needed.


----------------------------------------------------------------------------


More ...

Sometimes just editing a post won't do, you need to completely delete it, using your knowledge that you have gleened from this tutorials, and using the query DELETE FROM news WHERE id = $_GET[id] you should easily be able to make a deletion script :D. If you have any problems, go shout in the forums.
 

 
   Print this page

Top Sponsor
Symantec\'s Norton SystemWorks 2006
Sponsors
CA
Sponsors
AdWords Dominator 125*125
Advertisting


Affiliates
VertexTemplates PHPFreaks CodeWalkers StarGeek DevScripts CGI & PHP Scripts PHP CMS Free Templates

Shopping Rebates   Sell It 4 You   Flash Page Counters   Get Insured
GPS Tracking Service   Charity Donate Info   Web Site Hosting   VOIP Service

Privacy Policy | Links | Site Map | Advertising

All content on OxyScripts.com is (©)2002-2007

 
Powered by Adrastea - Version 1.0.0. Copyright © Rune Solutions, 2004-2005