我在 mysql 表中有大量报价,我想每 10 秒在我的网页上显示一个随机报价。我用谷歌搜索了这个问题,但我确实找到了如何从数据库中选择一个随机记录作为第 1 部分以及如何每 10 秒更改一次页面上的文本作为第 2 部分,因此当我将两个部分链接在一起时,我会显示随机报价页面,但它每 10 秒重复一次。所以请帮忙。
<?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>