How to take the average of several sets of numbers (assigning variable names?)

I'm still fairly new at PHP, and am trying to put together a program for use by myself and a small number of other people. A very simple program already exists and works. I'd like to add some ways that people can retrieve their own data so that they can analyze it and learn from it.

People basically just enter a numerical rating at different times of the day. It could be, for example, the outdoor temperature at any given hours. (It isn't, but that is probably the best example.)

I'd like for people to be able to ascertain their average temperature rating for, say, mornings during the first two weeks of April, 2013. I pretty much understand how to restrict the query to (a) hours between, say, 6 a.m. and noon; and (b) days between April 1 and April 14, 2013.

A twist arises because some days will have more than one morning rating, e.g., temp is 50 at 6:00 a.m.; 60 at 10:00 a.m.; and 65 at 11:45 a.m. So I need to average those temps and then have just one figure for that date.

Bear in mind, please, that I'm still very much a beginner and therefore tend to think in quite simplistic ways! This is basically only my second project and I'm content to proceed in very elementary ways, even if they are not the most efficient. (for now, anyway)

So - I'm imagining a conditional that will say something like: if there is more than one entry for these hours on any given day, then add those entries and average them, and assign the result to variable $morning_temp; else (if there is only one entry for these hours), then assign that entry to variable $morning_temp. Then I would want to add them and take their average.

I've boxed myself in, however -- I'm assigning all the temps to the same variable name!

Can anyone tell me please how to get myself out of this corner?

For the moment, at least, I'd like to stay fairly close to this paradigm, as it's one that I understand and could, with some effort, probably implement. There are probably ways that would be much spiffier and more elegant, but there is a significant risk that I would not, at present, understand them.

I'm hoping that someone can nudge me in the right direction.

Thank you!

04-25-2013, 01:05 PM

Anne_Arbor

Should I be creating an array? is that the step that I'm missing?

(I'm just working at the concept level for the moment - not asking anyone to write code for me.)

Thank you for any help.

04-25-2013, 01:30 PM

Anne_Arbor

Two additional small points / questions:

1: Continuing to think about this, I see that my proposed averaging method for a given morning is rather crude. That is, because the temps are not equi-distant in time, their 'average' will be somewhat inaccurate. That is okay for the present. Further refinements can be looked at in due course.

2: If I'm assigning values to an array, is there any way that I can keep them in chronological order? That is, if I'm taking the average of three values for April 1; then have just a single value for April 2; but again want to take the average of several values for April 5 -- right now, I'm envisioning an if/else set-up that would deal with April 1 and April 5 first, and only then take up April 2 in the else clause. Is there some way that I can have the results show up in chrono order (April 1, April 2, April 5)?

Thank you for your patience thus far!

04-25-2013, 01:31 PM

Anne_Arbor

Query: would I construct a more complex if/else statement, in order to keep things chronological?

.

04-25-2013, 06:56 PM

rtrethewey

It sounds to me like you need to create a database that would hold this information, which could then be used to analyze it as you like. PHP makes working with databases pretty easy, and hosting services always provide tools for creating them, so you can start off simply enough.

But the coding questions will boil down to defining your needs more clearly. It's easy enough to suggest methods that would be appropriate to average temperatures for a given time of day across a specified time period, but that might not work for your particular situation. Could you be more specific about what you're trying to do?

04-25-2013, 07:05 PM

Anne_Arbor

rtrethewey,

Actually, I already have a database and a small program that works. That is, users can enter data and also retrieve their own data. I'd like to make it more useful to them by enabling them to retrieve just chunks of it. I really did my best in explaining what would help me go to the next step. For example, am I correct in thinking that I need to use an array?

I really can't think how I could make my question clearer. I'm pretty sure I described the problem accurately.

04-25-2013, 07:07 PM

Anne_Arbor

Again, I'm not asking anyone to write the code. I have some idea of what to write. I'm asking for some informed guidance on the direction I should be looking in. If someone can confirm that basic direction, then I'll have more confidence that I'm not wasting my time heading in the wrong direction.

04-25-2013, 07:11 PM

rtrethewey

Well then, I'd repeat my suggestion about working with a database and then proceed with the methods you describe. Good luck!

04-25-2013, 07:17 PM

Anne_Arbor

I'm hoping that someone will be willing to take my questions seriously and give me an idea of whether I am headed in the right direction. My questions are sincere and I have done my best to pose them articulately and intelligently.

Thank you to anyone who may be inclined to help!

04-25-2013, 07:19 PM

Anne_Arbor

For example - getting back to the idea of an array. Is that the direction I need to go in?

Is my idea of averaging by using an if/else conditional a reasonable one?

Is there some way that I can keep the values for the various dates in chronological order -- even though they will be processed in two different 'batches' (so to speak) using the if/else format?

04-25-2013, 10:35 PM

NogDog

If, in fact, this is coming from a DB query, then I would agree with the other response that you make the averaging part of the query. Since I don't know how the data is stored in the DB, I can't be sure of the exact approach, but if we assume the observation_timestamp is one column and temperature another, it might be something like....

Code:

SELECT
DATE(observation_timestamp) AS obs_date,
AVG(temperature) AS avg_temp -- this is where the magic happens :)
FROM your_table_name
WHERE
observation_timestamp BETWEEN '2013-03-30 00:00:00' AND '2013-04-05 00:00:00'
-- the 2 date strings above would actually be variables from your PHP script
AND
TIME(observation_timestamp) BETWEEN '06:00:00' and '12:00:00'
-- above time strings might also be variables?
GROUP BY obs_date -- where the rest of the magic happens
ORDER BY obs_date ASC

Databases are designed from the ground up to be optimized for handling and processing data, so it's good to leverage that efficiency when you can. :)

04-25-2013, 10:47 PM

Anne_Arbor

NogDog, I really appreciate it that you have replied. Unfortunately I don't understand your reply because it is couched in terms that are different from my question. Are you saying that I don't need to use arrays? Your reply uses code that I am not familiar with, and I cannot tell if it takes into consideration my concerns about averaging the data for a single day and then using that result to calculate an average for the entire period.

04-25-2013, 10:48 PM

Anne_Arbor

If someone would address my actual questions, I would probably understand the answers. :)

04-25-2013, 11:33 PM

NogDog

I'm saying that doing the averaging in your PHP application code is not the place where I would do it: I would do it in the DBMS. It's both more efficient (faster) and simpler to do (though "simpler" is in the mind of the beholder, obviously).

If for some (stubborn ;) ) reason I just didn't want to do it in the database layer, I would probably build a results array in at least 3 dimensions, then loop through it or, in this example, leverage the array_walk() function to do the looping.