Building a Weblog: Part 2 / Page 4
[previous] [next]
Building a Weblog: Part 2
Showing the Entry
With the validation complete, you can display the blog entry. This code looks very similar to the code on index.php. First, add the header HTML:
You now need to determine which type of query to run. Inside the validation checking code, validentry is set to either 0
(if no variable was supplied) or to the ID
of the entry to view.
If validentry
is set to anything other than 0, the query is simple—ask for that specific blog entry. If the value is 0, however, the query should load the latest blog entry (the same behavior as index.php):
Send the query to the database with the mysql_query()
function:
Now you can present the results to the user. This code is virtually identical to the code that you wrote on index.php to present the blog entry. The only real difference is that the subject of the entry is not linked to anything.
The main blog entry section is now complete.
Showing Blog Comments
To display the comments, first create a SQL query that will get the comments for the current blog entry:
You count the number of rows again with mysql_num_rows()
and use the value to determine if any comments have been posted. If numrows_comm
is equal to 0, the text No comments
is displayed; otherwise, the else
block of code is executed.
Inside the else, you perform two basic functions: display each comment and then create an anchor on each one that can match up with the anchors referenced by index.php.
At the top of the else
block, you first set i
to 1; this variable is used as a counter to implement the anchors. Next, the while
loop iterates through each comment from the query and creates the anchor. A link is created with a name
attribute set to the text comment
, with the value of i
appended (resulting in, for example, comment2
). The main comment fields are then displayed in a similar way to the main blog entry. Finally, the i
variable is incremented by 1, preparing for the next comment's output.
[previous] [next]
URL: