Link to home
Start Free TrialLog in
Avatar of macgruder
macgruder

asked on

counter question

I have a counter system which counts the number of hits to a dynamic page.

so http://whatever.com/member/view.php?id=12

ups id 12 in a mysql database within a table called counter. Easy enough so far.

What I want is a system which has a record of number of hits today, this week, totals for each of the last 12 months, grand total.

I'm sure there is an elegant solution to this!
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

If all you do is increase a count for the ID, then you will not have enough data to track when these hits occur.

You can ask your ISP for the log of the hits to your site and filter the ones you want. From there, you can use one of many different log reporting tools or roll your own.

If you want to create your own log, then you will need to store each hit seperately with the date/time against the hit and any other information you want (agentstring, IP, etc).

Your reporting would be basically a select on this table with a date/time range applied, giving you an hour, a day, a week, month, year, 17.5 minutes, whatever.

You CAN use a text file (like a normal log file) and this would probably be quicker, but if you have a LOT of simultaneous hits, you MAY miss some. Using a DB would be safer (assuming queuing or multi-threading updates).

Richard.
Avatar of macgruder
macgruder

ASKER

The script can choose any information such as time as I want. The question is what is the most efficient way to store the information. I'm not interested in a detailed stat analysis of my site - I have that with Webalizer. Rather just something that says X is the most popular this week/today or whatever.

I have this in my script

               $query = mysql_query("update $table SET count = LAST_INSERT_ID(count+1), date = now() where name = '$id'") or return_error_png("no up");
               
What I need is someway that at the beginning of the month totals are reset to zero and old months are shifted back one if you see what I mean.

i think there must be a nice way to do it, but I'm too busy to work it out right now.

I have my own server so i can do anything basically - within the small limits of my ability of course ;-)
Ok. I understand this.

Say you have the following table.

UniqueID
Range Name
DateTimeStart
DateTimeEnd
Count

This table could then hold the following data ...

ID Name Start End Count
1,Year,2002 2002-01-01 00:00:00.000,2002-12-31 23:59:59.999,0
1,July 2002,2002-07-01 00:00:00.000,2002-07-31 23:59:59.999,0
1,Friday 26th July 2002,2002-07-26 00:00:00.000, 2002-07-26 23:59:59.999,0

Your SQL would then be something like ...

UPDATA table
SET count=count+1
WHERE start <= now() AND now() <= end

The "clever" bit is that you can insert into the table any record for any range you want. You can do this every time the table is loaded, so you can add in today, the month, this year.

You might want to hold another table holding the last entries added for each range. So you can retrieve the list of known range types (and when they where last added) see if the range has expired. If it has then insert a new record for that range. You then increment the count for all ranges that match.

I'm not sure I've explained myself very well, but I hope this makes sense.

Richard.
Ranges Table

ID, Type, LastEndInserted
1,     Year,   2002-12-31 23:59:59.999
2,  Month,  2002-07-31 23:59:59.999
3,  Daily,  2002-07-26 23:59:59.999
4,  Weekly, 2002-07-27 23:59:59.999

You can query this table as ...

SELECT * FROM ranges WHERE LastEndInserted < NOW()

This will give you a list of all the new range records you need to create.

Say we did this tomorrow, based on the above data we would get ...

3,  Daily,  2002-07-26 23:59:59.999

If we had no updates until Monday we would get ...

3,  Daily,  2002-07-26 23:59:59.999
4,  Weekly, 2002-07-27 23:59:59.999


You may want to create empty records for missed days, but I think you shouldn't. Waste of space.

ASKER CERTIFIED SOLUTION
Avatar of td234
td234

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks guys,
I'll be a little busy over the next few days, but I'll try to test these two and let you know how I got on and then to the points!
Thanks a lot for all the comments, guys. Thom's answer is easy to implement into my code.