Now that we have had some article in our dB, it is now time to show the article to the world. Plus we need to show the comment to them
Let us make a page called as ‘article.php’ and the article is fetched by getting the a url as ‘article.php?articleid=1’ This will fetch the article # 1 and is stored in the DB as articleid=1
If the articleid query is not given then the page has to display all the aritcle listing with the article subject as an link to ‘article.php?article=[the articleid]’
<?php //if articleid query is not set, show all the article listing if (!isset($_GET['articleid'])) { $conn = mysql_connect('localhost', 'username', 'password'); mysql_select_db('yourdb'); $sql = "select * from articles;"; $result = mysql_query($sql); echo('<table boder=0 cellpadding=3 cellspacing=1>'); while ($row = mysql_fetch_assoc($result)) { echo('<tr>); echo('<td>); echo('<a href="aricle.php?articleid='.$row['articleid'].'"><b>'.$row['articlesubject'].'</b></a>'); echo('<tr>); echo('<td>); echo($row['articlesubject']); } echo('</table>') mysql_close($conn); } else { $conn = mysql_connect('localhost', 'username', 'password'); mysql_select_db('yourdb'); $sql = "select * from articles where articleid=" . $_GET['articleid'] . ";"; if (!($result = @mysql_query($sql))) { echo('Sorry We are not able to process your request'); } else { //now display the data $row = mysql_fetch_assoc($result); echo ("<p><b>" . nl2br(htmlspecialchars($row['articlesubject'])) . "</b>"); echo ("<p>".nl2br(htmlspecialchars($row['articletext']))."<p>"); ?> Add Comment <table> <form name='comment' action'addcomment.php' method=post > <input type=hidden name='comdate' value="<?php echo(date("Y-m-d H:i:s"))?>"> <input type=hidden name='articleid' value="<?php echo($_GET['articleid'])?>"> <tr><td> Subject<input type=text name='comsubject' /> <tr><td> User<input type=text name='comuser' /> <tr><td> <textarea name='comtext' rows=15 cols=50></textarea> <input type=submit value='Post Comment' /> </form> </table> <?php //now show all the comments for this article $sql = "select * from comments where articleid=" . $_GET['articleid'] . ";"; $result = mysql_query($sql); if (mysql_num_rows($result)<1) { echo('No comments posted for this article'); } else { while ($row = mysql_fetch_assoc($result)) { echo('<p>'); echo( htmlspechialchars(stripslashes($row['comsubject'])) . ' by ' . htmlspechialchars(stripslashes($row['comuser'])) . ' on ' date("d M Y h:i a", strtotime($row['comdate'])) . '<br>'); echo( nl2br(htmlspechialchars(stripslashes($row['comtext']))) ); } } mysql_close($conn); } } ?>
This now completes the code for viewing the article, the page also displays a form which as a subject textbox, a username textbox and a textarea for comment text.
When user clicks the ‘Post Comment’ button, the data is ‘post’ fed to the page ‘addcomment.php’