Richard Hedges
Richard Hedges

Reputation: 1188

JS to cycle through MySQL results?

Currently I have a small piece of PHP code that gets one random row from my table.

<?php
    if (!$query = @mysql_query("SELECT * FROM testimonials ORDER BY rand() LIMIT 1")) {
        echo 'Error: '.mysql_error().'';
    } else {
        while ($q = mysql_fetch_array($query)) {
            $quote = $q['quote'];
            $author = $q['author'];
            echo $quote;
            echo '<br />- <strong>'.$author.'</strong>';
        }
    }
?>

What I'd like to do instead is have it load one when the page loads, then fade out and fade in to another one from the database.

Upvotes: 0

Views: 187

Answers (1)

Wilbo Baggins
Wilbo Baggins

Reputation: 2751

Either load several rows upfront and use JS to cycle through them in the browser, or use AJAX to request a new row from the database every time you cycle through to the next one.

I would advise the first method as it lessens the amount of queries to your database and the number of HTTP requests to your web server.

Upvotes: 1

Related Questions