Re: Arrays - check my understanding?
| From: | Chris Hobbs | Date: | Wed, 07 Nov 2001 23:16:52 +0000 |
| Subject: | Re: Arrays - check my understanding? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-73823@lists.php.net to get a copy of this message | ||
FWIW, this worked very well, and sped my script up incredibly.
I did get one good suggestion to change my increment to:
$hits["$date"]["$hour"] += 1;
This seems to have the same effect, but makes it clearer that the value is being incremented.
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'];
--
_______ ___ _ ____ _____
Chris Hobbs / ____\ \ / / | | |/ ___\| __ \
Head Geek | (___ \ \ / /| | | | (___ | | | |
WebMaster \___ \ \ \/ / | | | |\___ \| | | |
PostMaster ____) | \ / | |__| |____) | |__| |
\____/ \/ \____/ \____/|_____/
http://www.silvervalley.k12.ca.us
chobbs@silvervalley.k12.ca.us