Page 1 of 1

[SOLVED]Easy sum...

Posted: Tue May 24, 2005 1:55 pm
by boonika

Code: Select all

<?php include('ban.php'); ?>
<?php include('config.php'); ?>
<?php
$SQL_statement = "SELECT * FROM countries";
$resultset = mysql_query($SQL_statement);
$totalpower = 0;
while ($data = mysql_fetch_array($resultset))
{
  $land = $data['country'];
  $ding2 = "SELECT * FROM game WHERE country = '$land'";
  $geg2 = mysql_query($ding2);
  $gekr = mysql_fetch_array($geg2);
  $count_rows = mysql_num_rows($geg2);


    for ($i = 0; $i == $count_rows; $i++) {
    $totalpower = $totalpower + $gekr['power'];
}
  echo "$totalpower";
}
?>
I have 2 tabels in my MySQL database:
- usertabel with country (name of the country), power and username
- countrytabel with all the country names

What I would like to get:
- A page where all the countries are displayed with their actual power (totalpower is the sum of all the power from all the users)...

I don't think this is difficult for you guys, it think you might find it directly... please help me
:wink:

If you have questions, plz reply.
Thanks :D

Posted: Tue May 24, 2005 2:19 pm
by Archy
Wasnt sure what you wanted achieved. Untested.

Code: Select all

<?php include('ban.php');
include('config.php');

$SQL_statement = "SELECT * FROM countries";
$resultset = mysql_query($SQL_statement);
$totalpower = 0;
while ($data = mysql_fetch_array($resultset))
{
  $land = $data['country'];
  $ding2 = "SELECT * FROM game WHERE country = '$land'";
  $geg2 = mysql_query($ding2);
  $count_rows = mysql_num_rows($geg2);
  $gekr = mysql_fetch_array($geg2);
 
 
    for ($i = 0; $i < $count_rows; $i++) {
      $totalpower = $totalpower + $gekr['power'];
    }
  echo "$totalpower";
}
?>

Posted: Tue May 24, 2005 2:58 pm
by boonika
no, it isn't what i want...
you should make a new script, as I think that my is totally wrong.

I need the sum of the powers of each member of the same country.
then you need to display the countryname and the countrypower...

is it clear or :?

thanks anyway

Posted: Tue May 24, 2005 3:25 pm
by Skara

Code: Select all

<?php
include('ban.php');
include('config.php');

$resultset = mysql_query("SELECT * FROM countries");
while ($data = mysql_fetch_array($resultset)) {
  $geg2 = mysql_query("SELECT * FROM game WHERE country='{$data['country']}'");
  $cpower = 0;
  while ($tmp = mysql_fetch_row($geg2)) $cpower += $tmp['power'];
  print("Country: {$data['country']} Power: {$cpower}");
}
?>
untested

Posted: Wed May 25, 2005 12:20 am
by boonika
Country: Belize Power: 0Country: Afghanistan Power: 0Country: Albania Power: 0Country: Algeria Power: 0Country: American Samoa Power: 0Country: Andorra Power: 0Country: Angola Power: 0Country: Antigua and Barbuda Power: 0Country: Argentina Power: 0Country: Armenia Power: 0Country: Aruba Power: 0Country: Australia Power: 0

It doesn't count the actual power...

Posted: Wed May 25, 2005 3:17 am
by Ollie Saunders
looks to me like the power values coming from the db are all zeros. So you'll never be able to add up a load of zeros.

Posted: Wed May 25, 2005 5:54 am
by boonika
nope, belgium must have something like 150

Posted: Wed May 25, 2005 6:12 am
by boonika
$tmp['power'] doens't display the power :?

Posted: Wed May 25, 2005 6:12 am
by phpScott

Code: Select all

<?php
include('ban.php');
include('config.php');
 
$resultset = mysql_query("SELECT * FROM countries");
while ($data = mysql_fetch_array($resultset)) {
  $geg2 = mysql_query("SELECT SUM(power) as pwr FROM game WHERE country='{$data['country']}'");
  $tmp = mysql_fetch_row($geg2));
  print("Country: {$data['country']} Power: {$tmp['pwr']}");
}
?>

Posted: Wed May 25, 2005 6:33 am
by boonika
phpScott wrote:

Code: Select all

<?php
include('ban.php');
include('config.php');
 
$resultset = mysql_query("SELECT * FROM countries");
while ($data = mysql_fetch_array($resultset)) {
  $geg2 = mysql_query("SELECT SUM(power) as pwr FROM game WHERE country='{$data['country']}'");
  $tmp = mysql_fetch_row($geg2));
  print("Country: {$data['country']} Power: {$tmp['pwr']}");
}
?>
Parse error line 8, I removed the ')'

Country: Belize Power: Country: Afghanistan Power: Country: Albania Power: Country: Algeria Power: Country: American Samoa Power: Country: Andorra Power: Country: Angola Country: Faroe Islands Power: Country: Fiji Power: Country: Finland Power: Country: France

Posted: Wed May 25, 2005 7:42 am
by boonika
OK, thanks for your help guys.

I managed to finally make it :D

phpScott gave me the right way but I had 2 small errors:

the parse error ")"
and the "mysql_fetch_row" is "mysql_fetch_array"

Code: Select all

<?php
include('ban.php');
include('config.php');

$resultset = mysql_query("SELECT * FROM countries");
while ($data = mysql_fetch_array($resultset)) {
  $geg2 = mysql_query("SELECT SUM(power) as pwr FROM game WHERE country='{$data['country']}'");
  $tmp = mysql_fetch_array($geg2);
  print("Country: {$data['country']} Power: {$tmp['pwr']}");
}
?>
Thanks :)