You are here

function image_get_random in Image 5.2

Same name and namespace in other branches
  1. 5 image.module \image_get_random()
  2. 6 image.module \image_get_random()

Fetch a random N image(s) - optionally from a given term.

1 call to image_get_random()
image_block in ./image.module
Implementation of hook_block.

File

./image.module, line 895

Code

function image_get_random($count = 1, $tid = 0) {
  if ($tid != 0) {
    $result = db_query_range(db_rewrite_sql("SELECT DISTINCT(n.nid), RAND() AS rand FROM {term_node} tn LEFT JOIN {node} n ON n.nid = tn.nid WHERE n.type='image' AND n.status = 1 AND tn.tid = %d ORDER BY rand"), $tid, 0, $count);
  }
  else {
    $result = db_query_range(db_rewrite_sql("SELECT DISTINCT(n.nid), RAND() AS rand FROM {node} n WHERE n.type = 'image' AND n.status = 1 ORDER BY rand"), 0, $count);
  }
  $output = array();
  while ($nid = db_fetch_object($result)) {
    $output[] = node_load(array(
      'nid' => $nid->nid,
    ));
  }
  return $output;
}