Languages :: PHP :: PHP: SUM VALUES WHICH HAS SAME ID |
|||
| By: excinc |
Date: 03/10/2003 00:00:00 |
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 | 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 | 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 | 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 | 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 |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








