Current location: Hot Scripts Forums » Programming Languages » PHP » How to use javascript and php to retrieve the time in the database and make it countdown to 0?


How to use javascript and php to retrieve the time in the database and make it countdown to 0?

Reply
  #1 (permalink)  
Old 12-05-11, 05:45 AM
Jalene Jalene is offline
New Member
 
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
X_X How to use javascript and php to retrieve the time in the database and make it countdown to 0?

I'm currently working on a countdown timer for a quiz. However, the admin can set a quiz at different time intervals like 15, 30, 45, 60 mins. I used this as a php variable and is stored under a timer field in database and for the countdown I'm using a JavaScript. The problem I'm facing now is I don't know how to echo out the php variables in using the JavaScript. So once the the admin set 15 mins and a student click the quiz the 15 mins timer will start to count down and when it reaches 0 it will says "Time is up! End of quiz. Answers have been submitted successfully."

*Do take note that the time varies and not always 15 mins? Now I can only display out the time but it's not moving/counting down!

Please help! ):
Let me know if i need to use other javascript.

Here's my code to display out the timer:-

<?php
include 'dbFunctions.php';
$q_subchapter_no = $_POST['q_subchapter_no'];
$subChapter_id = $_POST['subchapter_no'];

$arrQuizzes = "SELECT timer FROM quiz
WHERE subchapter_no = '" . $subChapter_id . "' ";

$result = mysqli_query($link, $arrQuizzes);

if ($result) {
$row = mysqli_fetch_row($result);
$row = $row[0];
} else {
$msg = "Error";
}


*Timer: <?php echo $row; ?>


Here's the javascript that im using. Your help will be greatly appreciated.

var javascript_countdown = function ()
{
var time_left = 10; //number of seconds for countdown
var output_element_id = 'javascript_countdown_time';
var keep_counting = 1;
var no_time_left_message = 'The time is up! Your answers will be submitted.';

function countdown() {
if(time_left < 2) {
keep_counting = 0; //when it's 1 second, it will stop counting down.
}

time_left = time_left - 1;
}

function add_leading_zero(n) {
if(n.toString().length < 2) {
return '0' + n;
} else {
return n;
}
}

function format_output() {
var hours, minutes, seconds;
seconds = time_left % 60;
minutes = Math.floor(time_left / 60) % 60;
hours = Math.floor(time_left / 3600);

seconds = add_leading_zero( seconds );
minutes = add_leading_zero( minutes );
hours = add_leading_zero( hours );

return hours + ':' + minutes + ':' + seconds;
}

function show_time_left() {
document.getElementById(output_element_id).innerHT ML = format_output();//time_left;
}

function no_time_left() {
document.getElementById(output_element_id).innerHT ML = no_time_left_message;
}

return {
count: function () {
countdown();
show_time_left();
},
timer: function () {
javascript_countdown.count();

if(keep_counting) {
setTimeout("javascript_countdown.timer();", 1000);
} else {
no_time_left();
}
},
//Kristian Messer requested recalculation of time that is left
setTimeLeft: function (t) {
time_left = t;
if(keep_counting == 0) {
javascript_countdown.timer();
}
},
init: function (t, element_id) {
time_left = t;
output_element_id = element_id;
javascript_countdown.timer();
}
};
}();

//time to countdown in seconds, and element ID
javascript_countdown.init(1500, 'javascript_countdown_time');


Or any better javascript to recommend?

Last edited by Jalene; 12-05-11 at 05:47 AM.
Reply With Quote
  #2 (permalink)  
Old 12-05-11, 10:36 AM
alxkls alxkls is offline
Newbie Coder
 
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
well instead of going through all that why don't you make it 10 times easier-add an column in the database which would be a timestamp(the time when the quiz or whatever it is you are doing expires) and then use jquery and one of the many pluginst that do just that:
Reply With Quote
  #3 (permalink)  
Old 12-06-11, 11:49 AM
Travis Weaver's Avatar
Travis Weaver Travis Weaver is offline
New Member
 
Join Date: Dec 2011
Location: Jacksonville, FL
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there
If you just want to keep track of how long a guy was on a page simply use the following, at the top of your php page:
PHP Code:

$time time();
$_SESSION['time_started'] = $time
Which stores the time (as a UNIX timestamp) that the page was loaded into the clients session.
To read it out and compare it with the time the next page is loaded, use the following:
PHP Code:

$oldtime $_SESSION['time_started'];
$newtime time();
$difference $newtime $oldtime
Well... and keeping track of the time remaining is a simple issue to solve:
Just keep a time remaining variable in your session, eg:
PHP Code:

      $time time();
      
$_SESSION['time_started'] = $time;
      
$_SESSION['time_remaining'] = 2700//45 minutes 
PHP Code:

      $oldtime $_SESSION['time_started'];
      
$newtime time();
      
$difference $newtime $oldtime;
      
$_SESSION['time_remaining'] = $_SESSION['time_remaining'] - $difference;
      if (
$_SESSION['time_remaining'] > 0)
      { 
          
$_SESSION['time_started'] = $newtime
         
//continue
      
} else {
         
//out of time
      

If you are happy with the answer, please give me a thanks.

Last edited by Travis Weaver; 12-06-11 at 11:54 AM.
Reply With Quote
Reply

Bookmarks

Tags
javascript, php


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hiring PHP Developer codestrong Job Offers & Assistance 5 08-15-11 03:43 PM
PHP inside JavaScript? pikaz JavaScript 20 07-10-10 12:55 AM


All times are GMT -5. The time now is 08:58 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.