|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Added results to an array
Hi.
I had some problems earlier with Nested searches and was helped out. Next I wanted to add those reulsts together for a total score. I did alot of searching and found that I needed to add the elements to an array and do an array_sum on them to get the result. However it isn't working. Here is what I have $query_ID = "select * from scores where shooter = '".$row_Shooters['id']."'"; $result_ID = mysql_query($query_ID); while($row_ID = mysql_fetch_array($result_ID)) { $total_score = array($row_ID['score']); echo $row_ID['score']; echo "<br>"; }echo (array_sum($total_score)); It returns the final search result only, so I think the array is overwriting itself with each result instaed of adding to it. I've been through the php manual and can't find anything. Thanks. |
|
#2
|
|||
|
|||
|
RE: Added results to an array
try it like this..
$total_score[] = $row_ID['score']; |
|
#3
|
|||
|
|||
|
RE: Added results to an array
You make things unnecessarily complicated.
Using array to store variables and then add it is not wise. Best way to do it is to use mysql aggregate function sum() with group by statement. If you intend to use php for addition, then here's how you can do it. $total=0; while($res=mysql_fetch_array($result)) $total+=$res['score']; or array version (bad idea) $total=array(); while($res=mysql_fetch_array($result)) array_push($total, $res['score']; echo array_sum($total); Dmitriy P.S. Make it as simple as possible |
|
#4
|
|||
|
|||
|
RE: Added results to an array
Thanks for that. Notepad's worked straight up but didn't reset for each shooter (my fault, not clear on what I wanted it for) and will definitly be used later. dimonemon's worked perfectly so thanks. I'm very new to PHP so forgive me doing things what appears a$$-backwards.
|
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > Added results to an array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|