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>