PHP News System Part 3 Comments (3)
by Wez Pyke - August 1, 2008 in PHP,Text Tutorials

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

require("dbconnect.php");

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.

 require("dbconnect.php");

$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
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Ping.fm
  • Tumblr
  • Twitter

Related posts:

  1. PHP gallery with no page reloads using jQuery For this tutorial we will be using the ImgBrowz0r class...
  2. Twitter-like pagination using CodeIgniter and jQuery If you use Twitter you'll notice that there is a...
  3. Build a Twitter-like site with CodeIgniter and jQuery Part 2 Don't forget to check out part 1 if you have...

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

Your Ad Here

3 Responses to “PHP News System Part 3”

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

  2. Tommy says:

    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.

  3. Tommy says:

    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

Leave a Reply