Skip to content
Advertisement

determine actions over period of time PHP MySQL

I don’t have any code or database to display, because I am in the planning stage, and I can’t figure out the correct way to do this.

I want to determine if a user has performed a specific action each day over the course of a week. If they have I want to perform an action. Each time there is a break in days, I need to reset.

So for example:

Day 1 | task performed 25 times
Day 2 | task performed 13 times
Day 3 | task performed 18 times
Day 4 | task not performed... start over at Day 1.
.....
Day 1 | task performed 3 times
Day 2 | task performed 11 times
Day 3 | task performed 14 times
Day 4 | task performed 7 times
Day 5 | task performed 3 times 
Day 6 | task performed 10 times
Day 7 | task performed 23 times

echo ‘You have successfully completed 71 tasks consecutively over a period of 7 days’;

I guess the real question becomes, what would be the best way to achieve this? Should I try to store 7 cookies based on date, and destroy all existing cookies if the newest cookie value is more than 24 hours old? Should I try setting and updating the date via the database?

Suggestions as potential solutions to achieve this is what I am looking for, keeping in mind this is a very database intensive app I am working on, so the less database calls IMO the better.

UPDATES

So I am trying to plan this out using a database and I am just running into issues at every angle.

If I try to set an incremental column there is no way to tell when the last update was.

If I set a table, its EXTREMELY complicated as I have to base the value off consecutive dates. So I can’t do a simple 7 day search, I have to test that each result is within 24 hours of the previous result set in the database, else how would I be able to distinguished someone who completed task on day 1, 3, and 7 vs the one who did it 1, 2, 3, 4, 5, 6, and 7.

Cleaning up the database is equally troublesome. How would I do a cleanup. I can’t simply test that the data is within 7 days of the current date because that doesn’t account for someone completing tasks on day 5 but not day six… and then on day 7… and equally its complicated because each user will have a different start date. So I may have started tasks today and another user 5 days ago. I can safely assume all dates over 7 days are expired, but that doesn’t really help determine breaks in days.

I am so confused how to actually accomplish this task.

Advertisement

Answer

there are several ways to do this.

if you’re going with the db route, you can have a reference from this SQL fiddle

I don’t know how db intensive it would be, but i think that would summarize the work needed if you are going this route. the advantage of this route is you dont have to delete or reset anything so you will have historical data, for example you can get how many “tries” the user do by changing the above query to having count(distinct d) < 7 (it would return as many rows as many the user tried to complete the task)

the other route is you can use file based data such as using JSON file on the server side for each user that doing the tasks. with this route, you need to maintain the data yourself for example you have this simple JSON structure:

{
   "day1": "2016-09-01",
   "last_task": "2016-09-03",
   "accumulated_task": "56"
}

what I mean by maintain the data yourself is, you need to update it every time changes are applied, such as if current date is 2016-09-05 and last_task is 2016-09-03, then you need to reset it. the advantage of this route is of course no db overhead, but many “manual labor”.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement