The attached code facilitates efficient retrieval of a single random quote from the database. It does so in a similar fashion to the method recommended by the API docs. I've tested it on my site and am hoping that the quotes maintainer could add it as a contrib module. I'd do it myself but my CVS access has been revoked.

Comments

pillarsdotnet’s picture

README.txt

$Id$

Quotes:Random is a simple helper module to facilitate efficient retrieval of a
single quote from among those quotes which are visible to the anonymous user.
This is useful for showing a single random quote somewhere on the page, without
the overhead associated with "order by RAND()".

The four functions intended for general use are as follows:

/**
 * Returns the nid of a randomly selected quote.
 */
function randomquote_nid()

/**
 * Returns a randomly-selected quote (summary view), linked to its full node,
 * and classed as "randomquote-link" for easy styling.
 */
function randomquote_link()


/**
 * Replaces the contents of a single page element with a randomquote_link().
 * This enables cached pages to have dynamic random quotes.
 *
 * @param string $element
 *   The jquery selector for a DOM page element whose contents will be replaced.
 *
 * Example usage (in MYTHEME/template.php):
 *   function MYTHEME_preprocess_page(&$vars) {
 *     // Insert a random quote as footer text
 *     randomquote_js('#footer');
 *   }
 */
function randomquote_js($element) {

/**
 * Replaces the contents of a single page element with a randomquote_link()
 * and periodically refreshes it with a new quote.
 *
 * @param string $element
 *   The jquery selector for a DOM page element whose contents will be replaced.
 *
 * @param int $delay
 *   The number of milliseconds to wait between subsequent refreshes of the quote.
 *
 * @param boolean $immediate
 *   Whether to insert the first randomquote immediately, with no delay.
 *
 * Example usage (in MYTHEME/template.php):
 *   function MYTHEME_preprocess_page(&$vars) {
 *     // Show a new random quote in the footer every thirty seconds.
 *     randomquote_js('#footer', 30000);
 *   }
 */
function randomquote_rotate_js($element, $delay, $immediate=TRUE) {

For efficiency's sake, randomquote_link() doesn't call any theme functions.
The following code will output a fully-themed random quote:

  echo theme('quotes_quote',array('node' => node_load(randomquote_nid())));
pillarsdotnet’s picture

StatusFileSize
new3.03 KB

Grr... one-character typo....

Revised.

pillarsdotnet’s picture

StatusFileSize
new3.17 KB

Slight improvements in code and comments.

pillarsdotnet’s picture

StatusFileSize
new3.77 KB

Further improved; quotes can load via javascript on cached pages.

pillarsdotnet’s picture

StatusFileSize
new4.26 KB

Quotes can now auto-refresh at periodic intervals.

pillarsdotnet’s picture

StatusFileSize
new4.28 KB

Discovered that the mysql RAND() function isn't very random. Substituted the PHP mt_rand() function and got much more even distribution of quotes.

pillarsdotnet’s picture

StatusFileSize
new4.36 KB

Further improvements; I don't know why, but sometimes MySQL fails when comparing an integer with a float.

pillarsdotnet’s picture

StatusFileSize
new4.28 KB

Performance enhancement.

pillarsdotnet’s picture

StatusFileSize
new4.33 KB

Fixed typo in query.

pillarsdotnet’s picture

StatusFileSize
new4.39 KB

Corrected handling of quotes with an empty "summary" field.

pillarsdotnet’s picture

Status: Needs review » Closed (won't fix)

I'm going to take a different approach altogether.