visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: PHP: SUM VALUES WHICH HAS SAME ID


By: excinc U.S.A.  Date: 03/10/2003 00:00:00  English  Points: 50 Status: Answered
Quality : Excellent
Hi! I have connected to oracle8 database with this code:

$connect = odbc_connect($db, $user, $pass) or die ($error);
$query = "SELECT * FROM TABLE";
$result = odbc_exec($connect, $query);
while(odbc_fetch_row($result))
{
echo odbc_result($result, 'id');
echo odbc_result($result, 'value');
}
odbc_close($connect);



And that code prints this result:

ID VALUE
21 10
23 20
23 1000
23 80
23 400
80 40
80 60


But I wan't to SUM values which has the same ID. How can I do the sum inside that 'database connection while' -loop?
I could do it in SQL statement but it isn't possible to do that way, believe me, I need it to be done in that while -loop.
So result should be this instead that above result:

ID VALUE
21 10
23 1500
80 100


This is pretty simple but I just can't solve it, maybe it's this friday and lazyness and fatigueness :-)

Regards,
excinc
By: VGR Date: 03/10/2003 20:30:00 English  Type : Answer
easy

use an associative array, like this :

$sumarray=array(); // luckily, variables are zeroed by PHP
$connect = odbc_connect($db, $user, $pass) or die ($error);
$query = "SELECT * FROM TABLE";
$result = odbc_exec($connect, $query);
while(odbc_fetch_row($result))
{
$sumarray[odbc_result($result, 'id')]+=odbc_result($result, 'value');
}
odbc_close($connect);
// display sums
foreach ($sumarray as $id=>$sum) echo "id=$id has sum=$sum
";


By: Karitz Date: 03/10/2003 23:28:00 English  Type : Comment
or

$sumarray=array(); // luckily, variables are zeroed by PHP
$connect = odbc_connect($db, $user, $pass) or die ($error);
$query = "SELECT DISTINCT(Id) FROM TABLE";
$result = odbc_exec($connect, $query);
while(odbc_fetch_row($result))
{
$res=odbc_exec($connect, "SELECT SUM(value) as sumval FROM TABLE");
$myrow=odbc_fetch_row($res);
$sumarray[odbc_result($result, 'id')]=$myrow['sumval']

}
odbc_close($connect);
// display sums
foreach ($sumarray as $id=>$sum) echo "id=$id has sum=$sum
";

By: VGR Date: 03/10/2003 23:55:00 English  Type : Comment
I don't think this second query is correct. There's not even a WHERE clause. The query will return the TOTAL sum(value) from all table's rows... whatever the ID they have...
By: snoyes_jw Date: 04/10/2003 00:48:00 English  Type : Comment
I know that you are using Oracle, but there's an interesting article about doing this sort of thing on the MySQL web site. It might not work for you depending on your table structure (many unique ID's would result in an extremely long query), but it might give you some ideas.

<A HREF="http://www.mysql.com/articles/wizard/print_version.html">http://www.mysql.com/articles/wizard/print_version.html</a>

Do register to be able to answer

EContact
browser fav
page generated in 300.850870 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page