Jump to content

Threaded Comment System


JustGags

Recommended Posts

I'm trying to create a simple threaded comment system in PHP.

 

The problem I'm having is that the first sub-comment begins an infinite loop and starts displaying endlessly.

 

I'm sure there is a simple fix for this.

 

<?php

function commentDisp($row, $level) {
   $userid = $row["userid"];
   $sql = "SELECT username FROM users WHERE userid='$userid'";
   $username = mysql_fetch_array(mysql_query($sql));
   ?>

   <div style="margin-right: 50px; height: 15px; background-color: #C9C9C9;
      margin-top: 15px; margin-left: <?php echo 300 + 50 * $level; ?>px; padding: 5px;">
   <p style="color: #444444; font-size: .75em;">
   Posted by: <?php echo $username["username"]; ?>
   |
   Posted on: <?php echo $row["timestamp"]; ?>
   </p></div>
   <div style="border: 1px solid #C9C9C9; margin: 0px; margin-right: 50px;
      margin-left: <?php echo 300 + 50 * $level; ?>px; padding: 5px;">
   <p style="font-size: .75em;"><?php echo $row["body"]; ?></p></div>
   <?php
}


function ifChildComm($commid) {
   $sql = "SELECT COUNT(commid) FROM comments WHERE parent='$commid'";
   $childCount = mysql_fetch_array(mysql_query($sql));

   if ($childCount["COUNT(commid)"] != '0') {
      return TRUE; }
   else {
      return FALSE; }
}


function fetchComm($parent,$propid) {
   $sql = "SELECT commid,userid,body,timestamp
           FROM comments
           WHERE propid='$propid'
           AND parent='$parent'";
   $commQuery = mysql_query($sql);

   while ($row = mysql_fetch_array($commQuery)) {
      commentDisp($row, 0);
      $commid = $row["commid"];

      while (ifChildComm($commid) == TRUE) {
         fetchComm($commid,$propid);
      }
   }
}


echo "<p style=\"margin-top: 30px; margin-left: 300px;
        font-weight: bold; text-decoration: underline;\">
        Discussion & Critique</p>";

fetchComm('0', $propid);

?>

 

Thanks in advance!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.