Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: PHP TEXT Rotate

  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PHP TEXT Rotate

    Hi, I'm making a text rotator that rotates text.
    The original script is here:http://www.javascriptbank.com/random...cript.html/en/

    My php for selecting from the database is here:
    PHP Code:
    <?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() ORDER BY RAND() LIMIT 6");
    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 when $events is printed out, for example would give ["Halloween","Christmas"]

    My javascript is:
    Code:
    <script>
    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 its not working. What's happening?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Is this condition true?
    PHP Code:
    // The JSON from PHP output here
      // Would look something like
      // ["Event 1","Event 2","Event 3"]
      var Quotations = <?php echo $events?>;
    If Quotations appears correctly in the generated source, this is not a PHP issue.

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    var Quotations = <?php echo $events; ?>;
    document.getElementById('textrotator').innerHTML = Quotation[which];

    are those the same variable ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Tags for this Thread

    Posting Permissions

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