mike12255 Posted February 18, 2009 Share Posted February 18, 2009 I sent a query and im tryingto find out the amount of posts inside a topic and i got the followng code to do so, but it returns the array size 0, anyone know how to fix?? <?php include ("connect.php"); $id = 22; $area = 'math'; /* Err... Let p be the number of posts, let m be the number of posts per page and let y be the number of pages. y=p/m*/ $postquery = "Select * from forumtutorial_posts WHERE parentid = $id AND area = '$area'"; $findamount = mysql_query($postquery) or die (mysql_error()); $array = array(); while ($row = mysql_fetch_assoc($findamount)) { $array = $row['posts']; } $testt = sizeof($array); echo $testt ?> Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 18, 2009 Share Posted February 18, 2009 Yes I do. It is because you are not doing the assignment you think you are. What you want to do is $array[] = $row['posts']; Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 18, 2009 Share Posted February 18, 2009 Well one other potential issue: your query might not be working. You should test that independently --- print_r() is your friend when debugging. Quote Link to comment Share on other sites More sharing options...
allworknoplay Posted February 18, 2009 Share Posted February 18, 2009 Yes I do. It is because you are not doing the assignment you think you are. What you want to do is $array[] = $row['posts']; He declared the array earlier: $array = array(); Do you think he still has to do this? $array[] Quote Link to comment Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 Yes I do. It is because you are not doing the assignment you think you are. What you want to do is $array[] = $row['posts']; He declared the array earlier: $array = array(); Do you think he still has to do this? $array[] Yes. Quote Link to comment Share on other sites More sharing options...
haku Posted February 18, 2009 Share Posted February 18, 2009 What he said. If you don't add the square brackets, it just turns it into a variable, and keeps overwriting that variable with each iteration of the loop. Quote Link to comment Share on other sites More sharing options...
mike12255 Posted February 18, 2009 Author Share Posted February 18, 2009 yeah i had to do array[], and it worked. Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 In fact an entirely better way to get a count would be to do it within your query. <?php include ("connect.php"); $id = 22; $area = 'math'; $sql = "SELECT COUNT(id) FROM forumtutorial_posts WHERE parentid = $id AND area = '$area'"; if ($result = mysql_query($postquery)) { if (mysql_num_rows($result)) { echo "There are " . mysql_result($result, 0) . " posts"; } } ?> Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 18, 2009 Share Posted February 18, 2009 In fact an entirely better way to get a count would be to do it within your query. include ("connect.php"); $id = 22; $area = 'math'; $sql = "SELECT COUNT(id) FROM forumtutorial_posts WHERE parentid = $id AND area = '$area'"; if ($result = mysql_query($postquery)) { if (mysql_num_rows($result)) { echo "There are " . mysql_result($result, 0) . " posts"; } } ?> Thorpe +1. I agree entirely -- if all you want is the count, then just do the count in the db. Only do the loop if you actually need the individual post row data for something else on the page. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.