PHP News System Part 3

If you haven't already viewed part 1 and part 2 then view them if you're new to PHP.

In part 3 of this tutorial you will learn how to display your posts, edit and delete them.

Creating The Main Page

We are going to create our main page that we will display our news on. First of all create a file called index.php in the parent folder of admin.

At the top of index.php type


Below the require function we're going to query the database for the 5 latest posts in the table.

$query = mysql_query("SELECT * FROM news_system ORDER BY id DESC LIMIT 5");

while( $row = mysql_fetch_array($query) ) {
$author = $row['author'];
$date = $row['date'];
$title = $row['title'];
$post = $row['post'];

echo $date . ' - <a href="viewpost.php?id=' . $id . '">' . $title . '</a><br>';
echo substr($post, 0, 50).'...';
echo '<br>';
echo 'By '. $author . '<br><br>';


What that code will do is display the last 5 entries if you want to display more then change the 5 after LIMIT.

if you take a look at when $post is echoed you'll notice that it is in the substr() function. What substr does is lets you limit the number of characters that are printed so in this case we start from the beginning of the variable so we use 0 and we want to display no more than 50.

View The Full Post

When the title of the post is clicked it will go to viewpost.php so create a file called viewpost.php.
The code for viewpost.php is similar to the main news page. The difference is that a while loop isn't used because its only displaying one entry so its not needed.


$id = $_GET['id'];

$query = mysql_query("SELECT * FROM news_system WHERE id='$id'");

$row = mysql_fetch_array($query);

$author = $row['author'];
$date = $row['date'];
$title = $row['title'];
$post = $row['post'];

echo '<h2>' . $title . '</h2>';
echo $post . '<br>';
echo 'Posted By ' . $author . ' on ' . $date;

The $id variable gets the ID from the URL so that we know which record we are selecting in the table to display.

In the fourth tutorial I'll show you how to edit and delete your posts.

Bookmark and Share
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Tumblr
  • Twitter

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

, ,

3 Responses to PHP News System Part 3

  1. Tommy October 2, 2008 at 1:22 pm #

    I've been following your tutorials and found a slight error that I was able to figure out, but I'm not sure if it's correct. I wasn't getting the id number in the viewpost.php?id= for my links and had to add $id to the array before it did show up.

  2. Tommy October 2, 2008 at 1:23 pm #

    oh. and thanks for the tutorials. I'm just learning php and so far I've learn more from your tutorials than most of the books I've bought and got bored with.

    Thanks again


  1. The Tutorial Blog » Blog Archive » PHP News System Part 4 - August 17, 2008

    [...] If you haven't checked out the previous tutorials then click on the names to view them. Part 1, Part 2, Part3. [...]

Leave a Reply