Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6774303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:46:15+00:00 2026-05-26T15:46:15+00:00

I’m writing a top 10 polling system. Pollsters vote weekly on their top 10.

  • 0

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:

  1. Be able to start at any time of the year.
  2. Be able to skip weeks.
  3. Not require shuffling of week numbers during calculations.
  4. 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).
  5. Use PHP, mySQL, file or other “standard” server items (except other programming languages or databases).
  6. Not require other software (e.g. “Install Software X, it does this!”).
  7. Pollsters are probably non-technical people, so asking them anything other than “Enter your top 10” or “edit your top 10” is not allowed.
  8. Be able to go over the end of the calendar year smoothly (e.g. Start in November and end in March).

Other Information:

  1. Pollsters will only be allowed to vote on a single day.
  2. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:46:15+00:00Added an answer on May 26, 2026 at 3:46 pm

    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:

    // 0-based
    $week_offset = floor(time() - strtotime("2011-11-02") / 7);
    

    I don’t know your polling algorithm. I’ll just demonstrate with a weighted poll (1-3 stars, 3 being best):

    | poll_id | user_id | week_offset | vote |
    |       7 |       3 |           0 |    1 |
    |       7 |       4 |           0 |    3 |
    |       7 |       5 |           0 |    2 |
    |       7 |       3 |           1 |    2 |
    |       7 |       4 |           1 |    2 |
    |       7 |       5 |           2 |    3 |
    |       7 |       5 |           5 |    1 |
    

    Running a query like

    SELECT 
      poll_id, 
      week_offset, 
      SUM(vote) as `value`,
      COUNT(user_id) as `count`,
      AVG(vote) as `average`
    FROM votes_table
    WHERE poll_id = 7
    GROUP BY poll_id, week_offset
    ORDER BY poll_id, week_offset;
    

    would give you something like

    | poll_id | week_offset | value | count | average |
    |       7 |           0 |     6 |     3 |       2 |
    |       7 |           1 |     4 |     2 |       2 |
    |       7 |           2 |     3 |     1 |       3 |
    |       7 |           5 |     1 |     1 |       1 |
    

    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?

    <?php
    // your database accessor of heart (mine is PDO)
    $query = $pdo->query($above_statement);
    $results = array();
    $previous_offset = 0;
    foreach ($query as $row) {
      // calculate offset distance
      $diff = $row['week_offset'] - $previous_offset;
    
      // make sure we start at 0 offset
      if ($previous_offset === 0 && $row['week_offset'] > 0) {
        $diff++;
      }
    
      // if distance is greater than a single step, fill the gaps
      for (; $diff > 1; $i--) {
        $results[] = array(
          'value' => 0,
          'count' => 0,
          'average' => 0,
        );
      }
    
      // add data from db
      $results[] = array(
        'value' => $row['value'], 
        'count' => $row['count'],
        'average' => $row['average'],
      );
    
      // remember where we were
      $previous_offset = $row['week_offset'];
    }
    // 0 based list of voting weeks, enjoy
    var_dump($results);
    

    You might also be able to do the above right in MySQL using a function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.