PDA

View Full Version : Struggling with an array


Alpal
07-28-2010, 02:58 AM
I use dreamweaver and have created a database, it is working fine. Have not used arrays before, spent some time doing your tutorial and searching the net for info on arrays. Am trying to create an array that is part of a summary page that will total 3 columns where the entry is not null.

Tried some simple arrays and they work
// Test 1 Output =45
$a=array(0=>"5",1=>"15",2=>"25");
echo array_sum($a);

// Test 2 Output =9.3
$value2= array(1.5,2.3,5.5);
echo array_sum($value2);

Tried this array and variations without success.
$VJSinc[]=mysqli->query( "SELECT Under_15_Vic, Under_12_Vic, Under_18_Vic FROM members;");
$sum=array_sum($VJSinc);
echo array_sum($VJSinc);

Any assistance would be greatly appreciated.:confused:

davidj
07-28-2010, 05:49 AM
You are trying to sum a string So it won't work

If you are wanting to sum up columns from a database use the sum function in your SQL

Select sum(column to sum) as col_sum from table

Alpal
07-28-2010, 06:42 AM
This is where I started out, it was suggested to me that using an array would be quicker. So if an array will not work is there a better way than:

SELECT SUM(members.Under_15_Vic) + SUM(members.Under_12_Vic) + SUM(members.Under_18_Vic)
FROM members

The problem with this is, if there is column/s that have no entries (all entries are null) then it does not work. ?

On the summary page I am developing there will be upwards of 70 recordsets, is that advisable?

Am interested in taking up the offer of learning php

Am busy with things for the next month or so. I run a site for a not for profit sporting association (Snooker & Billiards) in Victoria, Australia http://www.vbsa.org.au/ and have been developing a database for same for the last 12 months or so.

Thankyou once again for your assistance

davidj
07-28-2010, 08:21 AM
SELECT SUM(members.Under_15_Vic) as u15 + SUM(members.Under_12_Vic) as u12 + SUM(members.Under_18_Vic) as u18

then you would reference the results using the aliases u15, u12, u18

The offer to teach you PHP was not part of the original post. Its my signature. I am willing to teach you though

edbr
07-28-2010, 08:24 AM
its a course that has helped many of us too