Re: Arrays - check my understanding?
| From: | David Robley | Date: | Thu, 08 Nov 2001 01:04:36 +0000 |
| Subject: | Re: Arrays - check my understanding? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-73829@lists.php.net to get a copy of this message | ||
On Thu, 8 Nov 2001 04:15, Chris Hobbs wrote:
> I haven't had much chance to use arrays yet, but I think they'll solve
> a problem I have with a reporting script I'm working on.
>
> Basically, I have a log of accesses being kept in a mysql table (trust
> me, this _isn't_ a db question :) with timestamp as one of the fields.
> I'm currently querying for counts of this field to fill in my report -
> however, I want more granular reporting, such as hourly, and my current
> design means a separate query for each hour of each day - in other
> words, hourly for 20 days means 480 queries to the database - ugly!
>
> So, my thinking is that I do one query for all dates, then loop through
> all of the results and stick them in a 2 dimensional array. This should
> be _ton_ faster.
>
> So my code would hopefully look something like the following. The line
> I really have a question about is the assignment to the array:
>
> // connect to db and query above
> while ($row = mysql_fetch_array($results)) {
> $time = $row['timestamp'];
> $date = substr($time,0,8);
> $hour = substr($time,8,2);
> $hits["$date"]["$hour"]++;
> }
>
> Would this give me an array $hits that would be accessible like the
> following?
>
> echo $hits['20011103']['12'];
I think it really might be a DB question :-) If you are just wanting to
get counts of hits grouped by hour and day, why not use the date/time
functions built into mysql?
Your query might go something like:
SELECT COUNT(timestamp) AS howmany, HOUR(timestamp) AS hourofday,
DAYOFYEAR(timestamp) AS daynum WHERE whatever GROUP BY daynum, hourofday
Might be a bit easier and probably lots quicker.
--
David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc
CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIA
Please Tell Me if you Don't Get This Message