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
boonika
Forum Newbie
Posts: 18 Joined: Tue May 24, 2005 1:46 pm
Post
by boonika » Tue May 24, 2005 1:55 pm
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
If you have questions, plz reply.
Thanks
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 » Tue May 24, 2005 2:19 pm
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 » Tue May 24, 2005 2:58 pm
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
Skara
Forum Regular
Posts: 703 Joined: Sat Mar 12, 2005 7:13 pm
Location: US
Post
by Skara » Tue May 24, 2005 3:25 pm
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 » Wed May 25, 2005 12:20 am
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...
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Wed May 25, 2005 3:17 am
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 » Wed May 25, 2005 5:54 am
nope, belgium must have something like 150
boonika
Forum Newbie
Posts: 18 Joined: Tue May 24, 2005 1:46 pm
Post
by boonika » Wed May 25, 2005 6:12 am
$tmp['power'] doens't display the power
Last edited by
boonika on Wed May 25, 2005 7:40 am, edited 1 time in total.
phpScott
DevNet Resident
Posts: 1206 Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.
Post
by phpScott » Wed May 25, 2005 6:12 am
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 » Wed May 25, 2005 6:33 am
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 » Wed May 25, 2005 7:42 am
OK, thanks for your help guys.
I managed to finally make it
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