note 97407 deleted from function.rand by danbrown

From: Date: Mon, 19 Apr 2010 14:08:26 +0000
Subject: note 97407 deleted from function.rand by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168410@lists.php.net to get a copy of this message
Note Submitter: bryan ---- Mike, Why not just use: $SQL = "SELECT * FROM HIGHLIGHTS ORDER BY RAND() LIMIT 1"; OR $SQL = "SELECT * FROM HIGHLIGHTS WHERE HIGHLIGHTS_ID IN (SELECT HIGHLIGHTS_ID FROM HIGHLIGHTS ORDER BY RAND() LIMIT 1"; For the 5 most recent you could: ORDER BY highlights_ID DESC LIMIT 5 OR add a timecode and: ORDER BY timecode DESC LIMIT 5 you could roll this into a function by: function GetHighlights($which=NULL, $how_many=1) { switch($which) { case 'highlights_ID': $order_by = "highlights_ID LIMIT ".$how_many; break; case 'timecode': $order_by = "timecode LIMIT ".$how_many; break; case default: $order_by = "RAND() LIMIT ".$how_many; break; } $SQL = "SELECT * FROM HIGHLIGHTS WHERE HIGHLIGHTS_ID IN (SELECT HIGHLIGHTS_ID FROM HIGHLIGHTS ORDER BY".$order_by; } then you can just insert GetHighlights() wherever you need a random highlight or GetHightlights('',5) or GetHighlights('highlights_ID', 5) etc.

« previous php.notes (#168410) next »