PDA

View Full Version : Random Quotes viewer



nawafpower
03-19-2012, 06:07 PM
1) Script Title: Random Quotes viewer

2) Script URL (on DD): http://www.dynamicdrive.com/forums/showthread.php?t=68085&highlight=random+quotes

3) Describe problem: I'm trying to include a random quotes from an external file (text or csv) and to make it refresh without the need for the whole page to refresh. inside an iFrame or what ever that could work, please help.

keyboard
03-19-2012, 09:57 PM
The url to the script is wrong.
Please replace it with the correct url.

Nile
03-19-2012, 10:39 PM
Here's a quick look at what that would look like:



<?php
function print_for_js($array, $arrName = "array"){
if(!is_array($array)) return false;

$js_array = "var $arrName = [";
foreach($array as $key => $value){
$js_array .= '"' . addslashes($value) . '",';
}
$js_array = substr($js_array, 0, strlen($js_array)-1) . "];";

return $js_array;
}
$quotes = explode("\n", file_get_contents("quotes.txt"));
?>

<div id="quotes"></div>

<script type="text/javascript">
<?php
echo print_for_js($quotes, "quotes");
?>
function changeQuote(){
document.getElementById("quotes").innerHTML = quotes[Math.floor(Math.random()*quotes.length)];
setTimeout(function(){ changeQuote(); }, 1000)
}
changeQuote();
</script>