ZoomCities IT Community Webmaster Forum

Full Version: News system in PHP/MySQL
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So you want a news system? This is a tutorial on how to make one. No ready-made script but a great (and handy) explanation.

1. Requirements
Basic knowledge of PHP and MySQL, a text editor (e.g. Notepad) and a browser.

2. Database Structure
The database will have to look something like :
table 'news'
- id INT AUTO_INCREMENT,
- author VARCHAR(xxx),
- title VARCHAR(xxx),
- message TEXT,
- date INT,
- PRIMARY KEY(id)

Notice that date is INT. That's because we'll be storing the value of php function time() which gives you a very big nummer that can be converted to dates.

2. Insert Script
The form to insert news should be only accesible by you (and/or assistents). That's why it's nice to include a small check, such as a password check. If you have a complete usersystem, you could include a usercheck to see if the user is e.g. an admin/moderator.
$password should be your desired password.

Code:
<?php
    $password = 'azerty';
    if(!empty($_POST['news_author']) || !empty($_POST['news_title']) || !empty($_POST['news_message']) || !empty($_POST['news_password']))
    {
        if(!empty($_POST['news_author']) && !empty($_POST['news_title']) && !empty($_POST['news_message']) && !empty($_POST['news_password']))
        {
            // You should check the password to see if it's correct.
            if($_POST['news_password'] == $password)
            {
                // We won't check for SQL Injection. That's something you have to do on your own. And it's not that important because the user already needed a correct password.
                $date = time();
                $query = "INSERT INTO news(id, author, title, message, date) VALUES('', '".$_POST['news_author']."', '".$_POST['news_title']."', '".$_POST['news_message']."', '".$date."'";
                // Insert the query into the database as you like.
            }
            else
            {
                echo '<p>incorect password</p>';
            }
        }
        else
        {
            echo '<p>please fill in all fields</p>';
        }
    }
<form action='insertnews.php' method='post'>
    <table>
        <tr>
            <td>Your name</td>
            <td><input type='text' name='news_author' /></td>
        </tr>
        <tr>
            <td>Title</td>
            <td><input type='text' name='news_title' /></td>
        </tr>
        <tr>
            <td>Message</td>
            <td><input type='text' name='news_message' /></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type='password' name='news_password' /></td>
        </tr>
        <tr>
            <td colspan='2'><input type='submit' /></td>
        </tr>
    </table>
</form>

3. Show Script
Now you have to show the data.

Code:
<?php
    $shownews_query = "SELECT * FROM news ORDER BY date DESC";
    $shownews_row = mysql_fetch_array($shownews_query);
?>
<table>
    <tr>
        <td>Author</td>
        <td><?php echo $shownews_row['author']; ?></td>
    </tr>
    <tr>
        <td>Title</td>
        <td><?php echo $shownews_row['title']; ?></td>
    </tr>
    <tr>
        <td colspan='2'><?php echo $shownews_row['message']; ?></td>
</table>

And so on, on, on, on and on....





So now you have seen the basics of how to make a news system. You can add as many features as you like, such as categories, showing the last 10 news items, ...

This is a great tut. but I have no knowledge of database programing .... u deserve reputation marks for that! its great. but i understand nothing from the first step.

How much time does it take to create that script?
and can it be integrated into a php page can pictures be added to posts.

yesar92 Wrote:
This is a great tut. but I have no knowledge of database programing .... u deserve reputation marks for that! its great. but i understand nothing from the first step.

How much time does it take to create that script?
and can it be integrated into a php page can pictures be added to posts.


The first step is how your database should look like. You can make databases in PHP, but it's easier in PhpMyAdmin.

If you have some knowledge of PHP, the script can be created in less then an hour. If you have a good knowledge of PHP you can do it in 30 minutes.

Offcourse it can be integrated, with some general knowledge of PHP you can do that without too much problems.

And pictures can be added using html.

It's really worth the effort to learn some PHP Wink

That script would be about five minutes easy for me. Not even. It's not filtering out any HTML, so you can do anything you want there. Also, time() returns a UNIX timestamp which is only 10 numbers long; not that bad.

EDIT: SQL isn't that hard:

Code:
CREATE TABLE `news` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`author` VARCHAR( 200 ) NOT NULL ,
`title` VARCHAR( 200 ) NOT NULL ,
`message` TEXT NOT NULL ,
`date` INT NOT NULL ,
INDEX ( `date` )
);

k so i create the database table , I now know how to do that using phpMyAdmin , but what about the script? Do I create a new php document then insert the code in step 3 (Insert Script) into that document and I call it news.php for instance.

Than integrate the show script code into my index.php page.

Am I doing well? or Am I missing somethings?

darkfate Wrote:
That script would be about five minutes easy for me. Not even. It's not filtering out any HTML, so you can do anything you want there. Also, time() returns a UNIX timestamp which is only 10 numbers long; not that bad.

EDIT: SQL isn't that hard:

Code:
CREATE TABLE `news` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`author` VARCHAR( 200 ) NOT NULL ,
`title` VARCHAR( 200 ) NOT NULL ,
`message` TEXT NOT NULL ,
`date` INT NOT NULL ,
INDEX ( `date` )
);

I'd use

Code:
CREATE TABLE `news` (
`id` INT NOT NULL AUTO_INCREMENT,
`author` VARCHAR( 200 ) NOT NULL ,
`title` VARCHAR( 200 ) NOT NULL ,
`message` TEXT NOT NULL ,
`date` INT NOT NULL ,
PRIMARY KEY(id)
);

I'm not a PHP/SQL-guru, so I don't really know what the difference is between yours and mine.



yesar92 Wrote:
k so i create the database table , I now know how to do that using phpMyAdmin , but what about the script? Do I create a new php document then insert the code in step 3 (Insert Script) into that document and I call it news.php for instance.

Than integrate the show script code into my index.php page.

Am I doing well? or Am I missing somethings?

Yes, you miss something. I didn't include a database connection. I never include ready-made scripts (point of a tutorial is to learn how to do something, not to use the copy-paste function once more)

K thanx I will try to complete that script now.
Thanx
Reference URL's