Page 1 of 1

Creating an array of numbers

Posted: Tue Aug 01, 2006 1:14 pm
by wbryan6
I have been working on a page that sums up numbers from an array, but the problem I am having is that my array length varies depending on the number of results returned from the database. Essentially is:

Code: Select all

$z = 0;
while(z < $count_from_db)
{
      $array_of_num[] = $z;
      $z++;
}

$to_sum = array($array_of_num);
At the moment I have to enter in values for $to_sum, ie

Code: Select all

$to_sum = array('0','1','2');
Is it possible to have the list of values I need generated into the code? Hopefully this description is thorough enough. Thanks.

Posted: Tue Aug 01, 2006 1:19 pm
by Grim...
MySQL's SUM function may be of some use...

Code: Select all

SELECT SUM(col_to_be_summed) AS the_sum FROM table

Posted: Tue Aug 01, 2006 1:22 pm
by Grim...
Or array_sum() may be worth looking up.

Posted: Tue Aug 01, 2006 1:25 pm
by feyd
I don't quite understand what ~wbryan6 is asking for.

Posted: Tue Aug 01, 2006 1:49 pm
by volka
Me neither.
The Sum of 1+2+3...+n is n(n+1)/2 and does not require array_sum ;)