I have a countdown script here
<h2 id="countdown"></h2>
<script>
var CountDownTimer;
CountDownTimer = function(dt, id) {
var _day, _hour, _minute, _second, end, selector, showRemaining, timer;
selector = document.getElementById(id);
end = new Date(dt);
_second = 1000;
_minute = _second * 60;
_hour = _minute * 60;
_day = _hour * 24;
showRemaining = function() {
var days, distance, hours, minutes, now, seconds;
now = new Date();
distance = end - now;
if (distance <= 0) {
CountDownTimer("09/27/2016 20:00", "countdown");
return;
}
days = Math.floor(distance / _day);
hours = Math.floor((distance % _day) / _hour);
minutes = Math.floor((distance % _hour) / _minute);
seconds = Math.floor((distance % _minute) / _second);
return selector.innerHTML = days + "days " + hours + "hrs " + minutes + "mins " + seconds + "secs";
};
return timer = setInterval(showRemaining, 1000);
};
CountDownTimer("09/27/2016 19:00", "countdown");
</script>
After the countdown is done, i want the query to select a random row from the database and restart the countdown to +1 hour. After 1 hour i want this to happen again.
This is the code that should select the random row, and this is the code i want to get impemented in the countdown script, if possible.
<?php $sql = "SELECT * FROM users ORDER BY RAND() LIMIT 1"; $result1 = $conn->query($sql);?>
Here is the code for the outcome from the last code
<?php
if ($result1->num_rows > 0) {
// output data of each row
while($row = $result1->fetch_assoc()) {
echo $row["username"];
}
} ?>
This code now works but the problem is that every time i refresh the page, a new random row is selected. What i want is that for every hour a new random row is selected.
when you generate random number, store in session using two variable, one for storing number and another for expire time.
Whenever user will visit your page, in backend it will be checked if value is set in session variable, if not then new value will be saved, else it will fetch number and check time. if time is expire new value should be generated.