HTML and CSS Reference
In-Depth Information
DISPLAYING THE COMMENTS WITH THE BLOG POST
The final thing we need to do to have our Generic Blog Commenting System up and running is
to update the blog page,
index.php
, to fetch and display the comments from the
Persistence
object.
•
Since this isn't a real blogging system we'll hard code the
$comment_post_ID
value to be
1
.
•
Include the
Persistence.php
class and fetch the comments from it. Comments are
associated with a blog post using a unique
$comment_post_ID
.
<?php
require
(
'Persistence.php'
);
$comment_post_ID
=
1
;
$db
=
new
Persistence
();
$comments
=
$db
-&
gt
;
get_comments
(
$comment_post_ID
);
$has_comments
=
(
count
(
$comments
)
&
lt
;
0
);
?>
Since we now have the
$comment_post_ID
accessible via PHP we should update the HTML
for the comment form to use this value.
&
lt
;
input type
=
"hidden"
name
=
"comment_post_ID"
value
=
"
<?php
echo
(
$comment_post_ID
);
?>
" id="
comment_post_ID"
/&
gt
;
We now have all the comments related to the blog post referenced by the
$comments
variable
we need to display them on the page. To do this we need to update the PHP in
index.php
to
iterate through them and create the required HTML.
&
lt
;
ol id
=
"posts-list"
class
=
"hfeed
<?php
echo
(
$has_comments
?
'
has-comments'
:
''
);
?>
"
&
gt
;
<
li
class
="
no-comments
">
Be the first to add a comment
.</
li
>
<?php
foreach
(
$comments
as
$comment
) {
?>
<
li
>
&
lt
;
article id
=
"comment_
<?php
echo
(
$comment
[
'id'
]);
?>
" class="
hentry"
&
gt
;