dongxiezhuo8852 2013-04-01 23:14
浏览 38

每10秒随机显示一次mysql查询结果

I have in mysql table ton of quotes and i want to display on my webpage every 10 seconds a single random quote of those. I googled this question but what i did find is how to select a random record from database as part 1 and how to change the text on page every 10 seconds as part 2, hence when i link both parts together i have the random quote displayed on page but it repeats itself every 10 seconds. so please help.

<?PHP

$dbh = new PDO('mysql:dbname='.$db_name.';host='.$db_host.';charset=utf8', $db_username, $db_password );

$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "SELECT message FROM `fb_messages` ORDER BY RAND( ) LIMIT 1";

try {

    $msg = $dbh->prepare($sql);
    $msg->execute();
    $msgtxt = $msg->fetchAll(PDO::FETCH_COLUMN, 0);
    $txt = json_encode($msgtxt);
    }

    catch(PDOException $e)
{
    echo $e->getMessage();
    die();
}
?>
<html>
<head>
<title>Rotating Text</title>
<script type="text/javascript">
var rotatingTextElement;
var rotatingText = new Array();
var ctr = 0;

function initRotateText() {
rotatingTextElement = document.getElementById("textToChange");
rotatingText[0] = rotatingTextElement.innerHTML; // store the content that's already on the page
rotatingText[1] = "Some Text";
setInterval(rotateText, 5000);
}
function rotateText() {
ctr++;
if(ctr >= rotatingText.length) {
ctr = 0;
}
rotatingTextElement.innerHTML = rotatingText[ctr];
}
window.onload = initRotateText;
</script>
</head>
<body>
<span id="textToChange"><?php echo $txt; ?></span>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dqdjfb2325 2013-04-03 09:02
    关注

    For the actual solution to work, you have to use ajax calls to load the new random each 10 seconds.

    Theory is as follows:

    • You use your code as given for the first random to show
    • Setup the setInterval to call rotateText every ten seconds (as is now)
    • but you change the body of rotateText to place an ajax call to your server, and load the next random quote

    Body of the function could be as follows (take care when implementing, I'm assuming presence of jQuery library for this, and the url has to be matched up, as detailed later):

    function rotateText() {
      $.ajax({
        url: 'your/url/for/the/generator/php',
        dataType: 'text',
        success: function (response) {
          rotatingTextElement.innerHTML = response;
        }
      });
    }
    

    For this you have to create a second php file, which does nothing, just loads a random quote and echoes it to the output, and point the url in the ajax call to that php.

    Copying from your code:

    <?php
    $dbh = new PDO('mysql:dbname=' . $db_name . ';host=' . $db_host . ';charset=utf8', $db_username, $db_password);
    
    $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $sql = "SELECT message FROM `fb_messages` ORDER BY RAND() LIMIT 1";
    
    try {
      $msg = $dbh->prepare($sql);
      $msg->execute();
      $msgtxt = $msg->fetchAll(PDO::FETCH_COLUMN, 0);
      $txt = json_encode($msgtxt);
    } catch(PDOException $e) {
      echo $e->getMessage();
      die();
    }
    header('Content-Type: text/plain');
    echo $txt;
    

    This does nothing else, just loads the next random quote, and displays it as a plain text line in the output.

    With the array that you define, you could do something like prefill the array with 10-15 elements, and rotate through them. (That would be true rotate, because the above mentioned ajax solution gives new random quote every ten seconds, with no particular order)

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么