infinity0
07-13-2012, 05:25 PM
Hi, I found a text rotator and am trying to get it to rotate text pulled from php with mysql.
The script I'm using is here: http://www.javascriptbank.com/random-text-rotator-script.html/en/
The PHP I have looks like:
<?php
date_default_timezone_set('America/Denver');
include("config.php");
// Assuming `when` is a real DATE or DATETIME data type in MySQL...
// compare to CURDATE() to get today's
$result = mysql_query("SELECT * FROM events WHERE `when` = CURDATE()");
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
if ($result) {
// array to hold all the output
$events = array();
while ($row = mysql_fetch_assoc($result)) {
// Add the event to your array
$events[] = $row['tag'];
}
// After building the array, encode it as JSON
// Later you'll echo this into your JavaScript in place of the array...
$events = json_encode($events);
}
?>
which if you print out $events returns a value like
["Halloween","Christmas"]
The Javascript I have to try and rotate this looks like this:
<script type='text/javascript'>
function rotateEvery(sec)
{
// The JSON from PHP output here
// Would look something like
// ["Event 1","Event 2","Event 3"]
var Quotations = <?php echo $events; ?>;
var which = Math.round(Math.random()*(Quotation.length - 1));
document.getElementById('textrotator').innerHTML = Quotation[which];
setTimeout('rotateEvery('+sec+')', sec*1000);
}
</script>
But the javascript wont rotate. Why is this?
The script I'm using is here: http://www.javascriptbank.com/random-text-rotator-script.html/en/
The PHP I have looks like:
<?php
date_default_timezone_set('America/Denver');
include("config.php");
// Assuming `when` is a real DATE or DATETIME data type in MySQL...
// compare to CURDATE() to get today's
$result = mysql_query("SELECT * FROM events WHERE `when` = CURDATE()");
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
if ($result) {
// array to hold all the output
$events = array();
while ($row = mysql_fetch_assoc($result)) {
// Add the event to your array
$events[] = $row['tag'];
}
// After building the array, encode it as JSON
// Later you'll echo this into your JavaScript in place of the array...
$events = json_encode($events);
}
?>
which if you print out $events returns a value like
["Halloween","Christmas"]
The Javascript I have to try and rotate this looks like this:
<script type='text/javascript'>
function rotateEvery(sec)
{
// The JSON from PHP output here
// Would look something like
// ["Event 1","Event 2","Event 3"]
var Quotations = <?php echo $events; ?>;
var which = Math.round(Math.random()*(Quotation.length - 1));
document.getElementById('textrotator').innerHTML = Quotation[which];
setTimeout('rotateEvery('+sec+')', sec*1000);
}
</script>
But the javascript wont rotate. Why is this?