PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Obadiah
Forum Regular
Posts: 580 Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:
Post
by Obadiah » Wed Nov 10, 2010 1:06 pm
say in trying to get the sum of 68 numbers
instead of doing
Code: Select all
$sum_of = $num1+$num2+$num3//till $num68
how can i use a loop to shorten it?...or can i?
McInfo
DevNet Resident
Posts: 1532 Joined: Wed Apr 01, 2009 1:31 pm
Post
by McInfo » Wed Nov 10, 2010 1:20 pm
Where do the $num variables come from? I would expect that many related values to be in an array rather than individual variables.
Obadiah
Forum Regular
Posts: 580 Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:
Post
by Obadiah » Wed Nov 10, 2010 1:37 pm
they are from an array...i take the numbers from the database and i want to add them up
McInfo
DevNet Resident
Posts: 1532 Joined: Wed Apr 01, 2009 1:31 pm
Post
by McInfo » Wed Nov 10, 2010 1:45 pm
Why not calculate the sum in the SQL query?
If that doesn't work, try array_sum().
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Wed Nov 10, 2010 1:46 pm
There are 10 types of people in this world, those who understand binary and those who don't
Obadiah
Forum Regular
Posts: 580 Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:
Post
by Obadiah » Wed Nov 10, 2010 2:25 pm
because user1 will enter 23 for question 1
and user2 will enter 67 for question 1
the answers to the same question are stored on different rows
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Wed Nov 10, 2010 2:28 pm
So, SUM them in SQL as McInfo suggested
There are 10 types of people in this world, those who understand binary and those who don't