Posted By

flatearthcomms on 10/15/11


Tagged

random


Versions (?)

Pick random field vale from mySQL (image filename etc)


 / Published in: PHP
 

  1. extract($_REQUEST);
  2.  
  3. $sql = "SELECT * FROM `site` WHERE `id`='1' ";
  4. $result = doSQL($sql);
  5. $r=mysql_fetch_array($result);
  6. extract($r);
  7.  
  8. $sqlBG = " SELECT `bgFile` FROM `site_bgs` AS r1 JOIN
  9. (SELECT (RAND() *
  10. (SELECT MAX(bgID) FROM random)) AS id)
  11. AS r2
  12. WHERE r1.id >= r2.id
  13. ORDER BY r1.id ASC
  14. LIMIT 1; ";
  15.  
  16.  
  17. $range_sql = " SELECT MAX(`bgID`) AS max_id , MIN(`bgID`) AS min_id FROM `site_bgs` " ;
  18. $range_result = doSQL($range_sql);
  19. $range_row = mysql_fetch_object( $range_result );
  20. $random = mt_rand( $range_row->min_id , $range_row->max_id );
  21.  
  22.  
  23.  
  24.  
  25. // Special code for Jane to test backgrounds
  26. if ( isset($bg) ) {
  27.  
  28. $sqlBG = " SELECT * FROM `site_bgs` WHERE `bgID`='$bg' ";
  29.  
  30.  
  31. } else {
  32.  
  33. $sqlBG = " SELECT * FROM `site_bgs` WHERE `bgID` >= $random LIMIT 0,1 ";
  34.  
  35. }
  36.  
  37.  
  38. $resultBG = doSQL($sqlBG);
  39. $rBG=mysql_fetch_array($resultBG);
  40. $bg = $rBG['bgFile'];

Report this snippet  

You need to login to post a comment.