简体   繁体   English

倒数完成后如何从MySql中选择随机行?

[英]How to select random row from MySql after countdown is done?

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. 倒计时完成后,我希望查询从数据库中选择一个随机行,然后将倒计时重新开始到+1小时。 After 1 hour i want this to happen again. 1小时后,我希望这种情况再次发生。

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. 如果时间到期,则应生成新值。

Just store ID of selected user in some additional table. 只需将所选用户的ID存储在其他表中即可。

For example in user_of_hour (hour, user_id). 例如,以user_of_hour(小时,user_id)为单位。

And check: 并检查:

  • if a user of an hour already stored then just get it by id 如果已经存储了一个小时的用户,则只需按ID即可获取
  • if not then proceed to the random selection 如果没有,则继续进行随机选择

Small advice: 小建议:

Do not use ORDER BY RAND() - it is very slow on big data sets. 不要使用ORDER BY RAND()-在大数据集上它非常慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM