www.webdeveloper.com
Results 1 to 5 of 5

Thread: can timer be set on the databse

  1. #1
    Join Date
    Jan 2014
    Posts
    4

    can timer be set on the databse

    Please can I set my quiz timer on the database? I have a timer but anytime that I click on the next question the timer refreshes and so can someone help me to have a code for a time that does not refreshes when a next button is clicked

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    969
    There are a few ways you could do this. You'll have to store something somewhere as you obviously found out a javascript timer will reset when changing pages.

    The simplest solution would be a cookie/localStorage (localStorage only works in modern browsers). This will allow you to set a specific timestamp inside of a cookie when the quiz is started. On all other pages you simply need to read the cookie and adjust your javascript timer accordingly.

    If you want to use PHP or a database it gets a little more complicated, but it would also be more reliable and secure as knowledgable users couldn't alter the timers. In that case you'd probably want to create a new row in your database with some unique identifier (which could be saved on the user's computer via cookie, or you could pass it in the URL). On the same row you would insert a specific timestamp (the start of the quiz). Each time a new question/page is loaded you would use their unique identifier to check the database and get their starting time, then update the timer accordingly. But frankly this just means you are calling your database each time a user goes on to the next question, which seems a bit necessary.

    An alternative would be AJAX. This would be about the same amuont of work as using PHP/MySQL, but would not have to call the database every time a user goes on to the next question.

    In any of these cases, if you want me or anyone here to write you some code to help you in this process, we kind of need to see some of your code. It's nearly impossible to write some new system without knowing anything about where it's going.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    You could store in $_SESSION.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    969
    Quote Originally Posted by NogDog View Post
    You could store in $_SESSION.
    Sometimes, it's the simple things...

    Disregard my over-complicated post. +1 to NogDog's solution.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    Quote Originally Posted by Sup3rkirby View Post
    Sometimes, it's the simple things...

    Disregard my over-complicated post. +1 to NogDog's solution.
    And depending on how the app is constructed, you might well combine an AJAX implementation on the front end that accesses the start-time/elapsed time from the server where it's stored in a PHP session. There's almost always more than one "right" way to do it.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles