I’m writing a top 10 polling system. Pollsters vote weekly on their top 10. How should I store their poll for each week? That is, how do I control what week the poll is in storage (mySQL) or in my PHP (5.x+) calculations?
(System #1) I’ve previously done this by having a file “week.txt” on the server that I set at 0 and then ran a cron job weekly to update +1. When I’m storing the poll data in the database, I’d just load the file and know what week it was. I’m looking for something more elegant.
The system must:
- Be able to start at any time of the year.
- Be able to skip weeks.
- Not require shuffling of week numbers during calculations.
- Be maintenance free by a human, other than a 1-off event (like saying “this is the start date”, “this is the end date”, once in a blue moon).
- Use PHP, mySQL, file or other “standard” server items (except other programming languages or databases).
- Not require other software (e.g. “Install Software X, it does this!”).
- Pollsters are probably non-technical people, so asking them anything other than “Enter your top 10” or “edit your top 10” is not allowed.
- Be able to go over the end of the calendar year smoothly (e.g. Start in November and end in March).
Other Information:
- Pollsters will only be allowed to vote on a single day.
- I’ll be running multiple polls at once that have no bearing on each other and thus may have different skip weeks.
My system I’ve used before won’t work because in order to skip weeks, it would need interaction and violate #4 and otherwise can’t skip weeks and thus violate #2.
I’ve thought of 2 systems but they have failures of parts of the above:
(System #2) Use PHP’s date(“W”) when the pollster votes. Thus, the first week they all get week #48 (for example), second week #49, so it would be easily to tell which week is what. The problem is that some polls will go over the calendar year, thus I would end up with 48, 49, 50, 51, 52, 1, 2, 3, 4 and violate #3 above. Also, if we skipped weeks, we could end up with 48, 49, 50, 1, 2, 3 which violates #2 and #8 above.
(System #3) Then, I had the idea to just store the date they enter the poll. I would set a date to calculate from the week prior to the first poll, thus, it would just need to calculate the difference between weeks and I’d know the week number. But there’s no easy way to skip weeks violating #2 unless we shuffle days which violates #3.
(System #4) I then had the idea that when a pollster first votes, we just record it as their week 1 vote. When they next vote, it’s week 2, and so forth. If they wanted to edit their poll (the same day), they’d just use the edit button and we wouldn’t record a new poll, because they’d have signaled it’s an edit. The only problem is if a pollster forgets a week, meaning I’d have to go in and correct the data (add a blank week or change the week number they voted but violate #4). This handles the skip weeks just fine. Maybe a cron job would solve this? If someone forgot, a cron job that runs after the poll closes would enter in a blank week. Could be programmed to see the max week number entered, if any userid didn’t have that week number, just enter in blank data.
If you can adapt any system above to meet all the criteria, that would be fine as well. I’m looking for a simple and elegant and hands-free solution.
Please ask for any other clarifying information.
When working with week numbers, you should keep in mind that 01.01.2012 is in week 52 (not 1). The question is if you want your polls to be fixed on calendar weeks, or 7-day-offsets from the poll-start-date. Consider your poll started on a friday and ended exactly 7 days after. You’d be crossing the calendar week barrier and thus have 2 “weeks” your users may vote.
I’d probably prefer the offset-approach, as strict calendar binding is usually not helpful anyways. Do you want to answer the question “what are the votes in calendar week 34” or “what are the votes in the third week of polling”?
Calculating the offset is quite simple:
I don’t know your polling algorithm. I’ll just demonstrate with a weighted poll (1-3 stars, 3 being best):
Running a query like
would give you something like
By now you’ll probably have noticed the gap 0, 1, 2, [3], [4], 5.
When grabbing that data from MySQL you have to iterate the results anyways. So where’s the problem extending that loop for a gap-filler?
You might also be able to do the above right in MySQL using a function.