Search
Left Quote    By failing to prepare, you are preparing to fail.
- Benjamin Franklin    
Right Quote
 
[login] | [Register]
 

Building a News Management System

by: sm0ke
Page: 1 of 5
(View All)

Introduction


The 3 things we need initially will be:
  • PHP (Hypertext preprocessor)
  • MySQL (Structured Query Language)
  • Apache (Web Server)
  • PHPMyAdmin is also a useful tool to use.
Got them? Right on with the show.

Create Table


Firstly we need to make sure we have a table created on our database:



The code above wll create a table on your MySQL Database called "news" with the following fields created:
  • id - The unique identifior of the news post
  • title - The title of your news post
  • content - The main body of your news post
  • author - The name of the author
  • date - The date and time of the news post (this will be formatted to a readable string using another file we create later on)

Login


So now that we have a table created we need a way to log into the database. This code will create a database connection. Save the file as "site.inc.php" in a directory called "site"



OK so now we have a database with a table called news, and a small file which contains the database login information along with the functions to log in and out of the database. The file is very important, because it eliminates the need to write your DB access details accross many files.

Admin Directory


Now need a way to fill the database with information, along with the ability to edit and delete information.

First off create a new folder in your site directory called "admin" or whatever you want. For this purpose, i am using admin as the admin directory.

The code below will call the pre-defined site.inc.php file, and display a form to add a new post:


Notice the code ends with } else { - That's because we still have to process the form once its been submitted. The line: <form action="<? echo $PHP_SELF; ?>" method="POST"> tells the browser that once the submit button has been pressed we can start to process the form contents using the next lot of code.

Validate

This peice will check for errors and validate the text input fields contained in the form:


There we just checked that the user has submitted the correct information in the forms.

Query


Next part of code will login to the database, then generate and execute the query to insert the information:


The above code snippets all together will present the user with a form, once all the fields have been validated after submission the contents of the form are sent to the relevant parts of the database. the insertion to the date field is defined in the sql query as NOW() this inserts a current timestamp into to database as opposed to writing in the date and time by hand.

Save the above as add.php in the admin directory.

Admin Features

If we want to edit or delete any of the posts we have submitted, were going to need an admin list of all the posts entered with links to either edit or delete that post.

The below code is the complete code for the admin list page (/admin/list.php):


Its not very pretty, but it displays all the latest posts in descending order, and presents two links next to each title to either edit or delete the post.

the code: <a href='ed.php?id=$send->id'></a> basically calls the edit script into play, while sending the "id" variable with the link. This allows us to edit or delete the post where id=xx.

Edit

The next bit of code is quite like the initial add script that we wrote earlier in the tutorial It connects to the databse and selects the record requested. So if you press edit on id 1, we see a form filled with all the information saved in the first news post.

The edit script - save as (/admin/ed.php):


There we logged into the database and collected all information on id number xx, then displayed the information in a form ready for editing. Next, we need to check if the form has been submitted, and were there any errors? If not, update the record in the db with the new values or halt if any error encountered.


That updated the databse for id number xx with the new values entered into the form.

Delete

And finally, the admin needs to delete posts. this page is only a few lines of code:



With the code sample shown above, we have connected to the database and asked it to delete the article with id number xx.

Display

Right, we now have a system to do the following:

Add, edit and delete news posts from within an admin section on your site. The part to do now, is display the news posts to your users. No point in adding news only to edit and delete it with no one able to see it.

Up till now your dir structure should look like this:

public_html
-----SITE
   site.inc.php

-----ADMIN
   index.php (create a index page to link to the add and list pages)
   add.php
   ed.php
   del.php
   list.php

The site inclusion file (site.inc.php) is sitting in its own directory under the public_html/htdocs/www/ / dir (this varies between servers). As well as calling this into the admin section to login to the database, we will also use the same file in the users section so the scripts to display news posts can also access the database.

First off, we'll create a file called (list.php) under your public html directory.

The list.php file works along the same way as your admin list.php file, but will display the title of the post, the day it was posted and a link to read the article.

list.php:


Pretty straight-forward, yes? The script requests every title in the database and displays it with a link to read the story, the limit on this list can be set by adding "LIMIT 0,x" with x being the max number of posts to display after the "id DESC" part of the query.

the link location "r.php?id=" will redirect to the page r.php and display the story selected from the list.

In the code sample below, we log into the database and collect all the information, then display it.

r.php:


Now, you may notice the inclusion of the 2 commands: nl2br(); and formatDate(); nl2br is a handy php function to convert line terminators to the valid html "<br>" tag. so no added html br tags need to be inserted in your posts.

the formatDate() is a function im going to introduce next to format the date element from an unformatted string to a nicely formatted readbale string.

file: site.funct.php - save in the site directory:


Conclusion

Ok - thats the end of this basic tutorial. You should now have a directory structure like:

public_html
index.php - (your index page)
   list.php
   r.php

-----SITE
   site.inc.php
   site.funct.php

-----ADMIN
   index.php - (your index page)
   list.php
   add.php
   ed.php
   del.php

You should also now have a basic understanding of how to conect to a database, add edit and delete information by using forms, and how to display the information to the end user.

The system here can be made to look nice with a little html formatting and work better with a little work and a bit of knowledge. I suggest following the tutorial. The code here does work, i have personally tested this on 3 different web servers by simply copying and pasting the code and following my own tutorial to the letter.

Enjoy.

sm0ke


1  |  2  |  3  |  4  |  5  |  
Next »


Comments:

  pewx
  Subject: "Admin" Date: Jan 24 2008 at 9:09 pm    
Hey m8,
I followed your guide step by step. But when i try using it says im not allowed to acces most of the files :(
Can ya help me out a little :)?

Regards
You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

542,381 Views
6 Total Comments
0 Rating of 5 ( Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Simple PHP Tutorial
· One File Website
· Pagination with PHP
· Building a Comments Script
· Complete Members System
· Variable Scope



"AllSyntax.com" Copyright © 2002-2017; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank