/    Sign up×
Community /Pin to ProfileBookmark

Temperamental variable

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;[/code]

but not this one:

[code=php]if($num_children > 0){[/code]

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 “.&nbsp;Parent Post: “;
echo $comment_post;
echo “&nbsp;Parent Comment: “;
echo $comment_parent;
echo “.&nbsp;Children: “;
echo $num_children;
echo “</div>”;
echo “<div class=’CommentFooter’>”;
echo “Rrreply?”;
echo “</div>”;

if($num_children > 0){
echo “<ul>tada!</ul>”;
}
}
}[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@bokehNov 08.2005 — What are you expecting to happen here:[code=php]if($num_children > 0){
echo "<ul>tada!</ul>";
} [/code]
mysql_num_rows() returns [B]boolean false[/B] on failure so you might want to consider the following instead:[code=php]if(!empty($num_children)){
echo "<ul>tada!</ul>";
} [/code]
×

Success!

Help @shanterusan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...