Results 1 to 3 of 3

Thread: Random Quotes viewer

  1. #1
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Random Quotes viewer

    1) Script Title: Random Quotes viewer

    2) Script URL (on DD): http://www.dynamicdrive.com/forums/s...=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.
    Last edited by nawafpower; 03-21-2012 at 06:23 PM.

  2. #2
    Join Date
    Mar 2011
    Location
    N 11 19' 0.0012 E 142 15' 0
    Posts
    1,591
    Thanks
    57
    Thanked 99 Times in 97 Posts
    Blog Entries
    4

    Default

    The url to the script is wrong.
    Please replace it with the correct url.
    Last edited by keyboard; 03-20-2012 at 02:15 AM.
    Posting Tips + FAQ | Issues? Feel free to PM me
    - keyboard1333[at]gmail[dot]com

  3. #3
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Here's a quick look at what that would look like:

    Code:
    <?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>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •