Expand|Select|Wrap|Line Numbers
- <?php
- include("db_connect.php");
- include("functions.php");
- ?>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <script type='text/javascript' src='jquery.pack.js'></script>
- <script type='text/javascript'>
- $(function(){
- $("a.reply").click(function() {
- var id = $(this).attr("id");
- $("#parent_id").attr("value", id);
- });
- });
- </script>
- <title>Threaded Comments</title>
- </head>
- <body>
- <div id='wrapper'>
- <ul>
- <?php
- $q = "SELECT * FROM comment WHERE parent_id = 0";
- $r = mysql_query($q);
- while($row = mysql_fetch_assoc($r)):
- getComments($row);
- endwhile;
- ?>
- </ul>
- <form id="comment_form" action="post_comment.php" method='post'>
- <label for="name">Name:</label>
- <input type="text" name="name" id='name'/>
- <label for="comment_body">Comment:</label>
- <textarea name="comment_body" id='comment_body'></textarea>
- <input type='hidden' name='parent_id' id='parent_id' value='0'/>
- <div id='submit_button'>
- <input type="submit" value="Add comment"/>
- </div>
- </form>
Expand|Select|Wrap|Line Numbers
- <?php
- function getComments($row) {
- echo "<li class='comment'>";
- echo "<div class='name'>".$row['name']."</div>";
- echo "<div class='comment_body'>".$row['comment']."</div>";
- echo "<div class='timestamp'>".$row['created_at']."</div>";
- echo "<a href='#comment_form' class='reply' id='".$row['id']."'>Reply</a>";
- /* The following sql checks whether there's any reply for the comment */
- $q = "SELECT * FROM comment WHERE parent_id = ".$row['id']."";
- $r = mysql_query($q);
- if(mysql_num_rows($r)>0) // there is at least reply
- {
- echo "<ul>";
- while($row = mysql_fetch_assoc($r)) {
- getComments($row);
- }
- echo "</ul>";
- }
- echo "</li>";
- }
- ?>
Expand|Select|Wrap|Line Numbers
- <?php
- include("db_connect.php");
- $name = mysql_real_escape_string($_POST['name']);
- $comment_body = mysql_real_escape_string($_POST['comment_body']);
- $parent_id = mysql_real_escape_string($_POST['parent_id']);
- $q = "INSERT INTO comment (name, comment, parent_id) VALUES ('$name', '$comment_body', '$parent_id')";
- $r = mysql_query($q);
- if(mysql_affected_rows()==1) {
- header("location: index.php");
- }
- else {
- echo "Comment cannot be posted. Please try again.";
- }
- ?>