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 Link to comment https://forums.phpfreaks.com/topic/194072-help-w-something-really-simple-plz/ 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? Link to comment https://forums.phpfreaks.com/topic/194072-help-w-something-really-simple-plz/#findComment-1021156 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' Link to comment https://forums.phpfreaks.com/topic/194072-help-w-something-really-simple-plz/#findComment-1021157 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 Link to comment https://forums.phpfreaks.com/topic/194072-help-w-something-really-simple-plz/#findComment-1021159 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. Link to comment https://forums.phpfreaks.com/topic/194072-help-w-something-really-simple-plz/#findComment-1021163 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! Link to comment https://forums.phpfreaks.com/topic/194072-help-w-something-really-simple-plz/#findComment-1021164 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 Link to comment https://forums.phpfreaks.com/topic/194072-help-w-something-really-simple-plz/#findComment-1021165 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); Link to comment https://forums.phpfreaks.com/topic/194072-help-w-something-really-simple-plz/#findComment-1021166 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! Link to comment https://forums.phpfreaks.com/topic/194072-help-w-something-really-simple-plz/#findComment-1021169 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.