Tutorials
Part 3: Display entries
-
Part 3: Display entries
Posted on June 10, 2004
Written by
Michelle (view more by Michelle)
Comments (0)
Filed under Build A Blog, Tutorials
Welcome to Part 3 of my Build A Blog tutorial.
We know now how to create the table, and insert our entries, but it doesn’t do us much good if we can’t display them. In this instalment we’re going to learn how to make an index page and a single entry page.
Now, first thing you’ll need to understand is that this code will create a simple, text-only page that displays your entries and almost nothing else. To add in a layout, you’ll want to include a header and footer, like this:
<?php include("/path/to/domain/header.inc"); ?>
For more info on headers and footers, read this tutorial.
Ok, let’s get started by creating our main page. First things first, open PHP:
<?php
Now, you’re going to need to connect to your database. If you remember, we use this code (don’t forget to put in your login info):
mysql_connect ('localhost', 'db_username', 'db_password') ;
mysql_select_db ('db_name');
For our MySQL query, we’re going to tell it to select everything from table php_blog, order by the timestamp (descending), and limit it to 5 entries. (You can change the limit to as many entries as you want to show on your main page).
$sql = "SELECT * FROM php_blog ORDER BY timestamp DESC LIMIT 5";
Tell the script to either select these entries or die:
$result = mysql_query($sql) or print ("Can't select entries from table php_blog.<br />" . $sql . "<br />" . mysql_error());
Ok, now that we’ve got the 5 most recent entries, let’s tell the script what to do with them. We’re going to use a while loop, which essentially says "while our query is true, do this:"
while($row = mysql_fetch_array($result)) {
First thing we want it to do is format our timestamp. We don’t want that odd number that is the timestamp showing on our site. So we’ll use the PHP date function to format it. If you want your date to look different than mine, check out the explanation at php.net.
$date = date("l F d Y", $row['timestamp']);
Here we are going to strip slashes from our title and entry. If you remember, when we posted our blog entry to the database, we had to escape and quotes so that they didn’t interfere with the database addition. To do this, slashes were added in front of the quotes. Here we’re removing those slashes - if we didn’t do this, we’d get text that would look like this: This is \"escaped\" text, it\’s got slashes in it.
$title = stripslashes($row['title']);
$entry = stripslashes($row['entry']);
Now let’s tell it to print our blog entry. We’ll have the title bold, then the entry, then "posted on this date" line, then a horizontal rule:
?>
<p><strong><?php echo $title; ?></strong><br /><br />
<?php echo $entry; ?><br /><br />
Posted on <?php echo $date; ?>
<hr /></p>
<?php
}
Notice that we closed PHP for a bit, which enables us to use plain HTML (except for our variables). This way, we can modify the formatting of our blog using normal HTML without it messing up the PHP. We open PHP again when we’re done with the HTML, or if we want to insert a variable.
Let’s now close PHP, since we’re done with the code:
?>
That’s it, that’s the whole code for our main page. Let’s look at it start to finish:
<?php
mysql_connect ('localhost', 'db_username', 'db_password') ;
mysql_select_db ('db_name');
$sql = "SELECT * FROM php_blog ORDER BY timestamp DESC LIMIT 5";
$result = mysql_query($sql) or print ("Can't select entries from table php_blog.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$date = date("l F d Y", $row['timestamp']);
$title = stripslashes($row['title']);
$entry = stripslashes($row['entry']);
?>
<p><strong><?php echo $title; ?></strong><br /><br />
<?php echo $entry; ?><br /><br />
Posted on <?php echo $date; ?>
<hr /></p>
<?php
}
?>
Now, for the single entry page, all you have to do is change the MySQL query to this:
$sql = "SELECT * FROM php_blog WHERE id='$id' LIMIT 1";
…and add this right above it:
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid ID specified.");
}
$id = (int)$_GET['id'];
This code prevents you from viewing the journal.php page without an ID. Without it, your page would throw up an error.
If you saved your single entry page as journal.php, to view (say) entry number 748, you’d go to:
http://yourdomain.com/journal.php?id=748
Understand, you can only view entries that you already have.
You might also want to take out the (now unescessary) horizontal rule, so your single entry page would look like this:
<?php
mysql_connect ('localhost', 'db_username', 'db_password') ;
mysql_select_db ('db_name');
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid ID specified.");
}
$id = (int)$_GET['id'];
$sql = "SELECT * FROM php_blog WHERE id='$id' LIMIT 1";
$result = mysql_query($sql) or print ("Can't select entry from table php_blog.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$date = date("l F d Y", $row['timestamp']);
$title = stripslashes($row['title']);
$entry = stripslashes($row['entry']);
?>
<p><strong><?php echo $title; ?></strong><br /><br />
<?php echo $entry; ?><br /><br />
Posted on <?php echo $date; ?>
</p>
<?php
}
?>
Technically, you now have a blog that you coded all by yourself! I know you want more features, like commenting, next and previous links, the ability to edit entries, and more, and we’ll cover those things in the coming installments.
Comments
Comments are closed for this entry.