Jump to content

Help w Something really simple PLZ!


Nas[wD]

Recommended Posts

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

$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

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

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 :D thanks again!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.