PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old June 24th, 2002, 01:36 PM
Marcus26 Marcus26 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 14 Marcus26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old June 24th, 2002, 05:45 PM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: Added results to an array

try it like this..

$total_score[] = $row_ID['score'];


Reply With Quote
  #3  
Old June 24th, 2002, 05:52 PM
dimonemon dimonemon is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 61 dimonemon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
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

Reply With Quote
  #4  
Old June 25th, 2002, 01:30 AM
Marcus26 Marcus26 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 14 Marcus26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Added results to an array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT