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
https://forums.phpfreaks.com/topic/57006-threaded-comment-system/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.