[SOLVED]Easy sum...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

[SOLVED]Easy sum...

Post 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
Last edited by boonika on Wed May 25, 2005 7:39 am, edited 1 time in total.
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post 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";
}
?>
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

Post 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
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

Post 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...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

Post by boonika »

nope, belgium must have something like 150
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

Post by boonika »

$tmp['power'] doens't display the power :?
Last edited by boonika on Wed May 25, 2005 7:40 am, edited 1 time in total.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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']}");
}
?>
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

Post 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
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

Post 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 :)
Post Reply