[PHP] Creating a news management system - PHP / MySQL Tutorials
 
  Buy Ebooks, Video Games, DVDs, Electronics and more !
Home | Help Forums | Web Design Tutorials | Free Swishmax Downloads Make Money Online Blog . Free Image and Photo Sharing and Hosting

 

 

Company

Home

Nework Details

Tutorials

SWiSH 2

SWiSHMax

Flash MX

PHP & MySQL

HTML / JScript

Adobe Photoshop

Corel Draw

Gimp

Miscellaneous

Downloads

Templates

Plugins

Fonts

Wallpapers

Free Images

Scripts and Codes

Products

E-Books

SWiSH BB

SWiSH Templates

SWiSH-DB Newsletter
Subscribe to our newsletter : GO
  Partners / Affiliates

   Free image hosting
   Web Design Pakistan
   Swish Templates
   SWiSH Climax
   Talkfreelance Community
   Template Desire
   Lite boards
   Script Sector
   GimpTalk
   13dots
   Idea Designs
   PHP Cafe
   Sposatoettore
   Free swishmax templates
   Swishzone

Manage your site with Flax Article Content Management System
[PHP] Creating a news management system
Category : PHP / MySQL | Level : Intermediate | Language : English
Ask Question | Read Comments | Post tutorial | Previous | Next

Web www.swish-db.com

The following tutorial will be a short introduction to how to create a news management type system where users submit news to your site. This tutorial will utilize php to process the data and mysql database to store the information. Shoutboxes & guestbooks can also be made with this tutorial. Please read carefully as there is a lot to learn here.

Test submit news here here
Test view news here

What this will do when finished is allow you to post news on your site without having to constantly upload new pages.

The first thing we need to do is create the database fields. Log into the MYSQL Administration tool. Either create a new database called NewsCMS or something similar if you have permission or use the one that your server administrator has given you.

Go into SQL section and run the following query to set up your database:

CODE


CREATE TABLE news (
  id int(10) unsigned NOT NULL auto_increment,
  postdate timestamp(14),
  author varchar(50) NOT NULL,
  title varchar(50) NOT NULL,
  news text NOT NULL,
  PRIMARY KEY (id),
  KEY postdate (postdate)
);



This query will create 5 tables in your database: id, author, title, news, author and date. The timestamp within postdate timestamp(14), will automatically print the date within the database without any extra php coding.

The next file we’re going to create is the config.php file. This will specify some variables for connecting to the database as well as the variables needed to display a specific amount of news items we would like to show on our homepage.

Open up notepad, crimson editor or which ever text pad you like use. Enter in the following information:

CODE


// database info

$dbhost = "localhost";
$dbname = "your database name";
$dbuser = "username";
$dbpass = "yourpass";

// connect to the database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

// display this number of news items on the page
// for now it will display 5 items on the page

$max_items = “5”;



The max items will be used later in this script. This number will determine the number of news articles that are displayed on your page. Right now lets keep it at 5 and see what happens. Please remember to edit all the variables or else something could go wrong.

Now we are going to set up the html form to post the news. This will be named nsubmit.html.3 input boxes are going to be created here named author, title, and news and the action will go to nsubmit.php which basically stands for news submit.

Here’s the code for the form.

CODE


<form name=”newsubmit” method=”post” action=”nsubmit.php”>
By: <input type=”text” name=”author”><br>
Title: <input type=”text” name=”title”><br>
News: <textarea name=”news” rows=”6” cols=”24”></textarea><br>
<input type=”submit” name=”submit” value=”Submit”>
</form>



As you have probably noticed, this is a pretty basic form. It’s your job to spice it up a bit.

nsubmit.php is the next file we are going to create. This file will process the data and then insert it into the database. This is going to be similar to the register.php I had you create in the previous tutorial about how to create a login and registration system.

So start out by connecting to the database. Instead of typing all the information again that we did in the config, we will simply include this file in our code. This is a much faster way of doing things, it prevents you from having to make multiple copies of your code.

CODE


<?php

// include the config file with the needed variables

include “config.php”;

// grab the variables from the form

$author = $_POST['author'];
$title = $_POST['title'];
$news = $_POST['news'];



and now we will insert the data into the database.

CODE


// insert the data into the database

$query = "INSERT INTO news (author, title, news)
VALUES('$author', '$title', ‘$news')";
mysql_query($query) or die(mysql_error());

echo "Thanks for submitting!";

?>




Simple huh? There wasn’t that much we needed to do except grab all the values from the form and then insert it into the news database! The comments within the code pretty much explain everything there.

There’s only one thing left to do, display the news. Lets name this file nview.php for now. You can then include this file within your mainpage if you want.

CODE


<?

// include the config file
include ’config.php’;

global $max_items;

// lets do a query for id, author, title, news & the date

$query = “SELECT id, author, title, news,” . “DATE_FORMAT(postdate, ‘%y-%m-%d’) as date “ . FROM news ORDER BY postdate DESC LIMIT $max_items”;
$result = mysql_query ($query);



In the above, we did a query and selected the items id, author, title news and date from the database. Postdate, ‘%y-%m-%d’) will specify the format to display the date in which is year, month day.

The final set of instruction is to keep grabbing data from the database and then set up some type of template to display it on your site. A while loop is needed to search through all the items until the max number of items is reached which is stored in the max_items variable above.

CODE


// fetch the values from the database

while ($row = mysql_fetch_assoc ($result)) {

// store the values into variables

$author = strip_tags( $row['author']);
$date = $row[‘date’];
$title = strip_tags( $row[‘title’])
$news = nl2br (strip_tags ($row[‘news’], ‘<a><b><i><u>’));

// make our template and
// display the data

echo “$title by $author on $date”;
echo “$news <br><br>”;

}

?>



Your news should now be displayed like:

This is the title by Tim on 2005/2/26
Now this is the news.

That’s it! You should now be able to view news and post news on your site.

Like always, I will now display the full php code of the above tutorial so you can examine it without all the interruptions.

nsubmit.php

CODE


<?php

// include the config file with the needed variables

include “config.php”;

//grab the variables from the form

$author = $_POST[‘author’];
$title = $_POST[‘title’];
$news = $_POST[‘news];

// insert the data into the databse

$query = ‘INSERT INTO news (author, title, news)
VALUES(‘$author’, ‘$title’, ‘$news’)”;
Mysql_query($query) or die(mysql_error());

echo "Thank you for submitting!";

?>



nview.php

CODE


<?php

// include the config file
include “config.php”;

global $max_items;

// lets do a query for id, author, title, news and the date

$query = “SELECT id, author, title, news,” . “DATE_FORMAT(postdate, ‘%y-%m-%d’) as date “ . FROM news ORDER BY postdate DESC LIMIT $max_items”;
$result = mysql_query ($query);

// fetch the values from the database
// we will use a while loop for this to get all values
// until it reaches the max_items.

while ($row = mysql_fetch_assoc ($result)) {

// store the values into variables

$author = strip_tags( $row['author']);
$date = $row[‘date’];
$title = strip_tags( $row[‘title’])
$news = nl2br (strip_tags ($row[‘news’], ‘<a><b><i><u>’));

// make our template and
// display the data

echo “$title by $author on $date”;
echo “$news<br><br>”;

}

?>



If I have any errors, please feel free to submit them.

Happy Coding smile.gif


© 2002-2005 Flaxweb Network | Article Manager | Gimp Tutorials Library | RAD C++ Library | Free C/C++ Sourcecode |
Sites of interest : Web Design Blog | Swish Templates | Swishmax Ebook | Photoshop Templates | Gimp Tutorials | Text Forum | Make Money Online