Gw Temp

Menu

Tutorial - 'News managment system' by skyrider

An item about General posted on

Blurb

A tutorial on how to make a script so you can make a system so you can update news from your webrowser.

Body

So you want a site that allows you to update news from your web browser like Cutenews, or Fusion, but want to make your own? Well then, read on! This tutorial will tell you exactly how to do so!

First off, I assume you know the basics of php/mysql. If you dont, then dont panic, for I will explain everything step by step to you!

Now, first you need to create your news table in MySQL to hold all your data. Make a new file and call it create.php. Enter the code as shown below:

//include the database connection file
include './database.php';
//Now create the table!
$create = "CREATE TABLE news (
author VARCHAR(20) NOT NULL,
email VARCHAR(20) NOT NULL,
title text NOT NULL,
avatar text NOT NULL,
news text NOT NULL )";
$result = mysql_query($create);
/*If the table could not be created, tell the user something went wrong */
if (!$result)
{
echo 'Couldnt create news table! >:O';
}
else
{
echo 'News table creation sucessfull!';
?>

Thats it for your creation file! Now let me break it all down for you.

include './database.php';

That includes your db connection information needed to create the table. Pretty self-explanitory :P.

$create = "CREATE TABLE news (
author VARCHAR(20) NOT NULL,
email VARCHAR(20) NOT NULL,
title text NOT NULL,
avatar text NOT NULL,
news text NOT NULL )";

Here, I created a variable which hold the table creation info for easier use if you wanted to call this funcion twice in the same script...which there isnt really a point in doing so...

$result = mysql_query($create);

Then I defined the variable $result to actually create the table.

if (!$result)
{
echo 'Couldnt create news table! >:O';
}
else
{
echo 'News table creation sucessfull!';

That basically just checks to see if there was an error in the script or your database connection info was invalid.

Alright! Now that you've created your table, it is now time to move on to the harder stuff.

Now, it is time to add news.

Create a new file, and name it add.php. In it, put the following:

//Was the submit button pressed?
if (!$submit)
{
?>

Title



Author



Avatar url


News



E-mail




}
else
{

include("./db.php");
$errorList = array();
$count = 0;

//Check for errors
if (!$title) { $errorList[$count] = "Invalid entry: Title"; $count++; }

if (!$news) { $errorList[$count] = "Invalid entry: News"; $count++; }


if (sizeof($errorList) == 0)
{
//If error check failed, then insert news into table
$query = "INSERT INTO news(title, avatar, news, author, email) VALUES('$title', '$pic', '$news', '$name', '$email' )";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

//News post sucessful! YAY
echo "New news post successful!";

}
else
{
//Errors occured :-/
echo "The following errors were encountered:
";
echo "
";
}
}
?>

Wow, you must be thinking, "WTF?! THIS IS WAY TO HARD FOR ME! SCREW THIS!". But wait just a minute! This is actually really REALLY simple! I wish I didnt have to, but just because I am a nice guy, I am going to explain it to you.

if (!$submit)
{
?>

Title



Author



Avatar url


News



E-mail




This basically says, if the submit button was NOT pressed, show the form.

}
else
{

include("./db.php");
$errorList = array();
$count = 0;

//Check for errors
if (!$title) { $errorList[$count] = "Invalid entry: Title"; $count++; }

if (!$news) { $errorList[$count] = "Invalid entry: News"; $count++; }

This code means, that if the submit button WAS pressed, then start checking for errors such as blank fields that are required to be filled in


if (sizeof($errorList) == 0)
{
//If error check failed, then insert news into table
$query = "INSERT INTO news(title, avatar, news, author, email) VALUES('$title', '$pic', '$news', '$name', '$email' )";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

//News post sucessful! YAY
echo "New news post successful!";

}

You can probably interpret this peice of code pretty easily. Or at least the beginning: if (sizeof($errorList) == 0). That means "If 0 errors turned up". Then we have some brackets {} The code inside of them inserts all of the info from the form into the proper rows of the table.


}
else
{
//Errors occured :-/
echo "The following errors were encountered:
";
echo "
    ";
    for ($x=0; $x {
    echo "
  • $errorList[$x]";
    }
    echo "
";
}
}

If you look at the beginninh of this code again, it says else. You all should know what ELSE means :P. If so, then your probably thinking, if there WERE errors in the script, do whats inside of the brackets {}. Well guess what? Thats right!


Thats it for the add script. Heh, and you thought it was HARD? Fortunately, you had me to explain it to you. ^_^

Now, for the most important part, and probably the easiest, the show news script which will show the news.
Create a new file and call it show.php inside of it, include the following code:

//include db connection file
include("./db.php.php");
//Get the news!
$query = "SELECT * FROM news DESC LIMIT 0, 5";
$result = mysql_query($query) or die ("Error in query: $query.". mysql_error());
// If there is new to show
if (mysql_num_rows($result) > 0)
{
//Fetch result
while ($row = mysql_fetch_object($result))
{
?>
//print the news!
title; ?> by: author; ?>

avatar; ?>
news; ?>


//If there is NO news to show, say it!
{
?> No available news at this time.
}
?>

Now, after I explain this very LAST piece of code to you, you will be on your way to having a new news system!

$query = "SELECT * FROM news DESC LIMIT 0, 5";
$result = mysql_query($query) or die ("Error in query: $query.". mysql_error());

That little script just grabs the news from the database and limits it to show 5 news articles on the page.

// If there is new to show
if (mysql_num_rows($result) > 0)
{
//Fetch result
while ($row = mysql_fetch_object($result))
{
?>
//print the news!
title; ?> by: author; ?>

avatar; ?>
news; ?>



That script checks to see if there is any news present, and if so, grab the info from the db, and print it!

//If there is NO news to show, say it!
{
?> No available news at this time.
}
?>

And then we have the last piece of code which basically means, if the is NO news in the database at the current time, tell the user that there is no news to print!


And there you have it, folks, a brand spanking new news system built by YOU! I hope you have actually learned something from this tutorial and not just copy+pasted it :P. Although, it dosnt really matter to me what you did.

Anywhoo, have fun with your new news script!