我这里有一个倒计时脚本如何在倒计时完成后从MySql中选择随机行?
<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>
倒计时完成后,我希望查询从数据库中选择一个随机行并重新启动倒计时+1小时。 1小时后,我希望这种情况再次发生。
这是应该选择随机行的代码,如果可能的话,这是我想要在倒数计时脚本中强制执行的代码。
<?php $sql = "SELECT * FROM users ORDER BY RAND() LIMIT 1"; $result1 = $conn->query($sql);?>
下面是最后一个码
<?php
if ($result1->num_rows > 0) {
// output data of each row
while($row = $result1->fetch_assoc()) {
echo $row["username"];
}
} ?>
此代码现在工作,但问题是,我每次刷新页面的时候,一个新的随机行选择结果的代码。我想要的是每小时一个新的随机行被选中。
你有一个例子吗? – JayJay
<?php session_start(); } else { //代码来生成新的数字..并将其存储在会话变量使用 $ _SESSION ['random_number'] = your_variable; } –