PHP Time Function - How Can I Count Link Hits In Preset Period Of Time?
I have a website and want to make some top-list over popular links(how many times they have been used )
Like:
Top-10 links this week
Top-10 links this month
I belive it must be something like this
If ($_GET someURL)
{add 1 to count colum in database}
But then there is the time problem. Eg. i want the colum in the DB to be set to 0 (nill) every time 7 days have pased.
So... how does the time function in PHP work, can someone give me a code that would set a database colum to 0(nill) eg. every 7 days or every monday ?
Do i need to update "date" in database every time someone uses a link, or is the date only inserted into the DB the first time the link is but into the DB ?
Thanks for the answers guys.... they did but me in the right direction, but i had to go to a forum for webmasters to get the details i needed. - http://www.webmaster-talk.com/
Like:
Top-10 links this week
Top-10 links this month
I belive it must be something like this
If ($_GET someURL)
{add 1 to count colum in database}
But then there is the time problem. Eg. i want the colum in the DB to be set to 0 (nill) every time 7 days have pased.
So... how does the time function in PHP work, can someone give me a code that would set a database colum to 0(nill) eg. every 7 days or every monday ?
Do i need to update "date" in database every time someone uses a link, or is the date only inserted into the DB the first time the link is but into the DB ?
Thanks for the answers guys.... they did but me in the right direction, but i had to go to a forum for webmasters to get the details i needed. - http://www.webmaster-talk.com/
I'd say a better way to do this is to store the counts in the database by link and also by date. Then you could use a query to add up the counts from the last 7 days. You'd never have to clear anything and you'd have archived stats and a lot of flexibility to change it from 7 days to something else.
You'd need to add a column to the table to represent the date and you'd modify your counting routine so that it increments the record with matching url and date.
Your SQL query would look something along these lines:
SELECT SUM(num_hits) AS total_hits FROM stats WHERE current_date() - date <=7 GROUP BY url ORDER BY total DESC;
Depending on what db you use and how you do your schema, you'll need to tweak a bit.
You'd need to add a column to the table to represent the date and you'd modify your counting routine so that it increments the record with matching url and date.
Your SQL query would look something along these lines:
SELECT SUM(num_hits) AS total_hits FROM stats WHERE current_date() - date <=7 GROUP BY url ORDER BY total DESC;
Depending on what db you use and how you do your schema, you'll need to tweak a bit.
I'd say a better way to do this is to store the counts in the database by link and also by date. Then you could use a query to add up the counts from the last 7 days. You'd never have to clear anything and you'd have archived
stats and a lot of flexibility to change it from 7 days to something else.
You'd need to add a column to the table to represent the date and you'd modify your counting routine so that it increments the record with matching url and date.
Your SQL query would look something along these lines:
SELECT SUM(num_hits) AS total_hits FROM stats WHERE current_date() - date <=7 GROUP BY url ORDER BY total DESC;
Depending on what db you use and how you do your schema, you'll need to tweak a bit.
You'd need to add a column to the table to represent the date and you'd modify your counting routine so that it increments the record with matching url and date.
Your SQL query would look something along these lines:
SELECT SUM(num_hits) AS total_hits FROM stats WHERE current_date() - date <=7 GROUP BY url ORDER BY total DESC;
Depending on what db you use and how you do your schema, you'll need to tweak a bit.
Hello,
Not a question to be asked in a not-so-serious forum like Yahoo answers. but i would like to try answering you.
2 Options.
#1. Keep every click stored in database.
This will ease many problems. This will keep track of every clicks in the database, so you can create scripts at a later time for further advanced options-- like most clicked link in a week in a particular week , or most clicked link in a particuar month ... But have a drawback. The database size increases as the number of clicks increases, if you have a site with millions of clicks per week, this will create a problem with normal databases.
If you are keeping every click recorded, there is no need to reset every week. just write a database query count(click from x date to y date). That is all.
#2. is like you said, make a count table, say, for being it flexible, we can make a record for a page/day. means, if a click is made on a link, the count relating to that page for that day will be added by one. So the maximum number of records you will have in a week is, 7*number of links. I think this structure can be used with same flexibility as i mentioned earler. I believe the second structure is much better one.
Take care. Please let me know if you like to know mroe about anything.
Take care,
Pachu
Not a question to be asked in a not-so-serious forum like Yahoo answers. but i would like to try answering you.
2 Options.
#1. Keep every click stored in database.
This will ease many problems. This will keep track of every clicks in the database, so you can create scripts at a later time for further advanced options-- like most clicked link in a week in a particular week , or most clicked link in a particuar month ... But have a drawback. The database size increases as the number of clicks increases, if you have a site with millions of clicks per week, this will create a problem with normal databases.
If you are keeping every click recorded, there is no need to reset every week. just write a database query count(click from x date to y date). That is all.
#2. is like you said, make a count table, say, for being it flexible, we can make a record for a page/day. means, if a click is made on a link, the count relating to that page for that day will be added by one. So the maximum number of records you will have in a week is, 7*number of links. I think this structure can be used with same flexibility as i mentioned earler. I believe the second structure is much better one.
Take care. Please let me know if you like to know mroe about anything.
Take care,
Pachu