Jump to content

[SOLVED] Need help summing 2 arrays


adicrst

Recommended Posts

i have a database and i want to output the total of users,bani(money) and pucte(points)

 

i made this script

 

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
   die ('Eroare conectare'.mysql_error());
  }

//selectarea bazei de date
mysql_select_db("rpg",$con);

//extragerea datelor
$result = mysql_query("SELECT * FROM user");

echo "<b>Membrii | Bani | Puncte</b><br>";
while($row = mysql_fetch_array($result))
  {
   echo $row['username']." ".$row['bani']." ".$row['puncte']."<br>";
  }

//total members
$membri = mysql_query("SELECT MAX(iduser) FROM user");
while($row_m=mysql_fetch_array($membri))
{ 
  echo "<br><br>Total membri: ".$row_m[0];
}

//total money
$bani = mysql_query("SELECT SUM(bani) FROM user");
while($row_b=mysql_fetch_array($bani))
  {
   echo "<br>Total bani: ".$row_b[0];
  }

//total points
$puncte = mysql_query("SELECT SUM(puncte) FROM user");
while($row_p=mysql_fetch_array($puncte))
  {
   echo "<br>Total puncte: ".$row_p[0];
  }


mysql_close($con);



?>

 

the output is like this

 

 Membrii | Bani | Puncte
adicrst 100 150
adi 150 150
alin 100 150


Total membri: 3
Total bani:350
Total puncte: 450

 

 

now i want to sum Total bani: 350 with Total puncte: 450 => 800. How do i do this ?

Link to comment
Share on other sites

can in this bit you not put

 


$running_total=0;
while($row = mysql_fetch_array($result))
 {
  echo $row['username']." ".$row['bani']." ".$row['puncte']."<br>";

$running_total= $running_total + $row['bani'];
$running_total = $running_total + $row['puncte'];
 }

 

 

and at the end just echo total

 

???????

 

 

declaire $running total as 0 then it goes through each member of the table adding all their bani and puncte together.

Link to comment
Share on other sites

You could do that all in one query

 

<?php

$query = "SELECT 
            MAX(iduser) as max_user, 
            SUM(bani) as max_bani, 
            SUM(puncte) as max_puncte, 
            (puncte + bani) as total 
          FROM users";
$result = mysql_query($query)or die(mysql_error());
$row = mysql_fetch_assoc($result);

print<<<HERE

Total membri: {$row['max_user']}<br>
Total bani: {$row['max_bani']}<br>
Total puncte: {$row['max_puncte']}<br>
Total bani and puncte: {$row['total']}

HERE;

?>

Link to comment
Share on other sites

i tried this

 

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
   die ('Eroare conectare'.mysql_error());
  }

//selectarea bazei de date
mysql_select_db("rpg",$con);

$query = "SELECT 
            MAX(iduser) as max_user, 
            SUM(bani) as max_bani, 
            SUM(puncte) as max_puncte, 
            (puncte + bani) as total 
          FROM users";
$result = mysql_query($query)or die(mysql_error());
$row = mysql_fetch_assoc($result);

print<<<HERE

Total membri: {$row['max_user']}<br>
Total bani: {$row['max_bani']}<br>
Total puncte: {$row['max_puncte']}<br>
Total bani and puncte: {$row['total']}

HERE;

?>

 

and i get Table 'rpg.users' doesn't exist

 

 

 

also with my old code i tried

 

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
   die ('Eroare conectare'.mysql_error());
  }

//selectarea bazei de date
mysql_select_db("rpg",$con);

//extragerea datelor
$result = mysql_query("SELECT * FROM user");

echo "<b>Membrii | Bani | Puncte</b><br>";
while($row = mysql_fetch_array($result))
  {
   echo $row['username']." ".$row['bani']." ".$row['puncte']."<br>";
  }

//total membri
$membri = mysql_query("SELECT MAX(iduser) FROM user");
while($row_m=mysql_fetch_array($membri))
{ 
  echo "<br><br>Total membri: ".$row_m[0];
}

//total bani
$bani = mysql_query("SELECT SUM(bani) FROM user");
while($row_b=mysql_fetch_array($bani))
  {
   echo "<br>Total bani: ".$row_b[0];
  }

//total puncte
$puncte = mysql_query("SELECT SUM(puncte) FROM user");
while($row_p=mysql_fetch_array($puncte))
  {
   echo "<br>Total puncte: ".$row_p[0];
  }

//total puncte si bani
$total=$row_b[0]+$row_p[0];

echo "<br><br>bani + puncte = ".$total;


mysql_close($con);



?>

 

and the output is

 

Membrii | Bani | Puncte

adicrst 100 150

adi 150 150

alin 100 150

 

 

Total membri: 3

Total bani:350

Total puncte: 450

 

bani + puncte = 0

 

Link to comment
Share on other sites

sorry for the double post, but i realized that at my own code the variables $row are not global and i dont know how to make it.

 

@pocobueno1388: i get this error

 

Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause

Link to comment
Share on other sites

<?php

$query = "SELECT 
            MAX(iduser) as max_user, 
            SUM(bani) as max_bani, 
            SUM(puncte) as max_puncte, 
            (puncte + bani) as total 
          FROM user
          GROUP BY iduser";
$result = mysql_query($query)or die(mysql_error());
$row = mysql_fetch_assoc($result);

print<<<HERE

Total membri: {$row['max_user']}<br>
Total bani: {$row['max_bani']}<br>
Total puncte: {$row['max_puncte']}<br>
Total bani and puncte: {$row['total']}

HERE;

?>

Link to comment
Share on other sites

You were just using way more queries than you had to.

 

When you got this error

Table 'rpg.users' doesn't exist

 

Thats pretty straight-forward that I accidentally made your table name "user" plural.

 

With your original code, all you had to do was add the two rows up...but even if you did that and it worked, the way you were doing it with all the queries isn't very efficient as you can see I gave you code that does in a lot less lines and is much easier on the database.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.