PHP/MySQL News with Comments - Displaying one item with comments
(Page 5 of 7 )
So, we have displayed all the news and we have displayed the comments. We need to tie it together. When a user clicks on the comments link from the main news page, we need a way to display the single news item with the comments. To do that, we will query the news table for the id of the news item we are looking for and display that news. Then, we will simply call the displayComments function to show the comments.
<?php function displayOneItem($id) { global $db; /* query for item */ $query = "SELECT * FROM news WHERE id=$id"; $result = mysql_query ($query); /* if we get no results back, error out */ if (mysql_num_rows ($result) == 0) { echo "Bad news id\n"; return; } $row = mysql_fetch_assoc($result); echo "<TABLE border=\"1\" width=\"300\">\n";
/* easier to read variables and * striping out tags */ $title = htmlentities ($row['title']); $news = nl2br (strip_tags ($row['newstext'], '<a><b><i><u>')); /* display the items */ echo "<TR><TD><b>$title</b></TD></TR>\n"; echo "<TR><TD>$news</TD></TR>\n"; echo "</TABLE>\n"; echo "<BR>\n"; /* now show the comments */ displayComments($id); } ?> |
Next: Adding comments >>
More Database Articles Articles
More By Matt Wade