I have the following function. Most of it works. Setting $parent=0 and then calling the function, it prints the Comments as requested. It is the $num_children variable that’s not working. It works in this line:
[code=php]echo $num_children;
but not this one:
[code=php]if($num_children > 0){
Furthermore, when it breaks, it shows my custom error message but not the mysql_error().
Please help, I am very confused.
[code=php]function comments($post_id, $parent){
$query_comments = “SELECT * FROM comments WHERE post=’$post_id’ AND parent=’$parent'”;
$array_comments = mysql_query($query_comments)
or die(“uh oh, array_comments didn’t work ” . mysql_error());
$num_comments = mysql_num_rows($array_comments)
or die (“uh oh, num_comments didn’t work “. mysql_error());
# Testing for comments
echo “<div class=’PostFooter’>”;
for($j=0; $j < $num_comments; $j++){
$comment_id = mysql_result($array_comments,$j,”id”);
global $comment_id;
$comment_author = mysql_result($array_comments,$j,”author”);
$comment_content = mysql_result($array_comments,$j,”content”);
$comment_timestamp = mysql_result($array_comments,$j,”timestamp”);
$comment_time_display = mktime(“H:i:s”, $comment_timestamp);
$comment_date_display = mktime(“F d, Y”, $comment_timestamp);
$comment_post = mysql_result($array_comments,$j,”post”);
$comment_parent = mysql_result($array_comments,$j,”parent”);
$query_children = “SELECT * FROM comments WHERE parent=’$comment_id'”;
$array_children = mysql_query($query_children)
or die(“uh oh, array_children flopped. ” . mysql_error());
$num_children = mysql_num_rows($array_children)
or die(“uh oh, num_children flopped. ” . mysql_error());
echo “<div class=’CommentHeader’>”;
echo $comment_author;
echo ” commented on “;
echo $comment_date_display;
echo ” at “;
echo $comment_time_display;
echo ” :”;
echo “</div>”;
echo “<div class=’CommentContent’>”;
echo $comment_content;
echo “. Parent Post: “;
echo $comment_post;
echo “ Parent Comment: “;
echo $comment_parent;
echo “. Children: “;
echo $num_children;
echo “</div>”;
echo “<div class=’CommentFooter’>”;
echo “Rrreply?”;
echo “</div>”;
if($num_children > 0){
echo “<ul>tada!</ul>”;
}
}
}