runnerjp Posted June 1, 2008 Share Posted June 1, 2008 hey guys... how would i cound number of topics for my forum thread...im gathering by the froum its in so for example WHERE forum = 'general' postid forum author title post showtime realtime lastposter numreplies parentid lastrepliedto 134 general Admin test 1 test me 212279922 1212279922 Admin 0 0 1212279922 132 Admin test 2 121227543412 12275434 Admin 0 130 0 130 general Admin welcome to forum welcome 1212275218 1212275218 Admin 2 0 1212275434 131 Admin hahah changed 1212275426 1212275426 Admin 0 130 0 as you can see this is how ,y db is set out so if someone replys to a post then parentid is filed in with the postid of that posrson but i caqn of the life of me figure out how i can get number of posts within the forum and number of threads :S Quote Link to comment https://forums.phpfreaks.com/topic/108223-solved-counting-posts-and-topics-for-forum/ Share on other sites More sharing options...
paulman888888 Posted June 1, 2008 Share Posted June 1, 2008 use tags please. Your information is really messy try useing tables [table][tr]hope ask again[/tr][/table] Quote Link to comment https://forums.phpfreaks.com/topic/108223-solved-counting-posts-and-topics-for-forum/#findComment-554734 Share on other sites More sharing options...
joquius Posted June 1, 2008 Share Posted June 1, 2008 First of all it's good practice to have a topic/post number field in each table, updated after each post to have less queries. For the moment, assuming the topics table name is "topics" $counts = mysql_fetch_array (mysql_query ("SELECT COUNT(`postid`) as count, SUM(`numreplies`) as sum FROM `topics` WHERE `forum` = 'general'")); $counts['count']; // topic num $counts['count'] + $counts['sum']; // posts (replies + first post in topic) Quote Link to comment https://forums.phpfreaks.com/topic/108223-solved-counting-posts-and-topics-for-forum/#findComment-554744 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 i tired $counts = mysql_fetch_array (mysql_query ("SELECT COUNT(`title`) as count, SUM(`id`) as sum FROM `forumtutorial_posts` WHERE `forum` = 'general'")); but i got Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/runningp/public_html/members/test2.php on line 4 Quote Link to comment https://forums.phpfreaks.com/topic/108223-solved-counting-posts-and-topics-for-forum/#findComment-554763 Share on other sites More sharing options...
joquius Posted June 1, 2008 Share Posted June 1, 2008 I think your fields are wrong, use mysql_query ("...") or die (mysql_error ()); Quote Link to comment https://forums.phpfreaks.com/topic/108223-solved-counting-posts-and-topics-for-forum/#findComment-554795 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 humm ok i did this <?php include '../settings.php'; $counts = mysql_query ("SELECT COUNT(`title`) as count, SUM(`postid`) as sum FROM `forumtutorial_posts` WHERE `forum` = 'general'")or die (mysql_error ()); $topic = $counts['count']; // topic num $posts =$counts['count'] + $counts['sum']; // posts (replies + first post in topic) echo $topic; echo $posts; ?> and all i get is 0 Quote Link to comment https://forums.phpfreaks.com/topic/108223-solved-counting-posts-and-topics-for-forum/#findComment-554799 Share on other sites More sharing options...
joquius Posted June 1, 2008 Share Posted June 1, 2008 Is this a topic table or do only have one table for everything? Is forumtutorial_posts the only table? Apart from that the SUM() needs to be on the numreplies not the id.. Quote Link to comment https://forums.phpfreaks.com/topic/108223-solved-counting-posts-and-topics-for-forum/#findComment-554800 Share on other sites More sharing options...
trq Posted June 1, 2008 Share Posted June 1, 2008 You never fetch the records from the $count array. Your also using reserved words as your aliases. <?php include '../settings.php'; if ($result = mysql_query ("SELECT COUNT(title) as cnt, SUM(postid) as sm FROM forumtutorial_posts WHERE `forum` = 'general'")) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); $topic = $row['cnt']; $posts =$row['cnt'] + $row['sm']; echo $topic; echo $posts; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108223-solved-counting-posts-and-topics-for-forum/#findComment-554801 Share on other sites More sharing options...
whizard Posted June 1, 2008 Share Posted June 1, 2008 you need to do something like this <?php include '../settings.php'; $result = mysql_query ("SELECT COUNT(`title`) as count, SUM(`postid`) as sum FROM `forumtutorial_posts` WHERE `forum` = 'general'")or die (mysql_error ()); $counts = mysql_fetch_array($result); $topic = $counts['count']; // topic num $posts =$counts['count'] + $counts['sum']; // posts (replies + first post in topic) echo $topic; echo $posts; ?> HTH Dan Quote Link to comment https://forums.phpfreaks.com/topic/108223-solved-counting-posts-and-topics-for-forum/#findComment-554802 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 ahh yes that worked a treat... ty Quote Link to comment https://forums.phpfreaks.com/topic/108223-solved-counting-posts-and-topics-for-forum/#findComment-554808 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.