note 97407 added to function.rand
| From: | bryan at osu1 dot php dot net | Date: | Sun, 18 Apr 2010 19:28:10 +0000 |
| Subject: | note 97407 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168373@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 64.71.164.2
Probable Submitter: 174.107.156.159
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/97407
Del: integrated -- https://master.php.net/note/delete/97407/integrated
Del: useless -- https://master.php.net/note/delete/97407/useless
Del: bad code -- https://master.php.net/note/delete/97407/bad+code
Del: spam -- https://master.php.net/note/delete/97407/spam
Del: non-english -- https://master.php.net/note/delete/97407/non-english
Del: in docs -- https://master.php.net/note/delete/97407/in+docs
Del: other reasons-- https://master.php.net/note/delete/97407
Reject -- https://master.php.net/note/reject/97407
Search -- https://master.php.net/manage/user-notes.php