Nas[wD] Posted March 4, 2010 Share Posted March 4, 2010 The code: $query = "SELECT * FROM forum_forums"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { extract($row); $total = $row["topics"] + $row["posts"]; } I know how to add the topics and posts per row, but i don't know how to add all the rows together I wanna add the fields 'topics', and 'posts' from all the rows together for a $ultratotal please help me somebody Quote Link to comment Share on other sites More sharing options...
schilly Posted March 4, 2010 Share Posted March 4, 2010 what type are 'topics' and 'posts'? are they integers? Quote Link to comment Share on other sites More sharing options...
Nas[wD] Posted March 4, 2010 Author Share Posted March 4, 2010 both are smallint(7)'s default '0' Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted March 4, 2010 Share Posted March 4, 2010 $query = "SELECT * FROM forum_forums"; $result = mysql_query($query) or die(mysql_error()); $count = 0; while($row = mysql_fetch_array($result)) { extract($row); $total = $row["topics"] + $row["posts"]; $count = $count + $total; } echo $count; //should be your ultratotal Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 4, 2010 Share Posted March 4, 2010 Yeah, you could do a query to add up all the subtotals, but since you are already getting all the individual records a better approach would be to calculate the subtotals as you process each record. Quote Link to comment Share on other sites More sharing options...
Nas[wD] Posted March 4, 2010 Author Share Posted March 4, 2010 I'll give it a try right now! Quote Link to comment Share on other sites More sharing options...
Nas[wD] Posted March 4, 2010 Author Share Posted March 4, 2010 Yeah, you could do a query to add up all the subtotals, but since you are already getting all the individual records a better approach would be to calculate the subtotals as you process each record. I thought the same thing, but I don't know how to lay it out in code Quote Link to comment Share on other sites More sharing options...
schilly Posted March 4, 2010 Share Posted March 4, 2010 this will be faster. should work. $ultratotal = mysql_result(mysql_query("SELECT sum(topics) + sum(posts) as ultratotal FROM forum_forums"),0); Quote Link to comment Share on other sites More sharing options...
Nas[wD] Posted March 4, 2010 Author Share Posted March 4, 2010 this will be faster. should work. $ultratotal = mysql_result(mysql_query("SELECT sum(topics) + sum(posts) as ultratotal FROM forum_forums"),0); Damn you guys are teaching me so much stuff, and they both work! Thank you so much!!!!! $totalmembers = mysql_result(mysql_query("SELECT COUNT(id) as totalmembers FROM members"),0); - also worked for my total members stat thanks again! 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.