note 96378 added to function.rand
| From: | mike at marylandwebsites dot net | Date: | Wed, 24 Feb 2010 02:42:56 +0000 |
| Subject: | note 96378 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-166307@lists.php.net to get a copy of this message | ||
Rather then counting the rows or running the RAND in my query, I find this to work just fine.
This code is used to display business highlights at random
Would love to get feedback on this
<?php
// First get the last id from the table
$SQL = "SELECT * FROM HIGHLIGHTS ORDER BY highlight_id DESC LIMIT 1";
$result = mysql_query( $SQL );
while( $row = mysql_fetch_array( $result ) ) {
$ending_id = $row["highlight_id"];
}
// I only want the 5 most recent entries
// So I just subtract 5 from the last ID
$starting_id = $ending_id - 5;
// Because I we don't want a nagitive number I just
// make sure that the starting ID is at least 1
if($starting_id <= 0){
$starting_id = "1";
}
// now I run the the $starting_id $ending_id at RAND
$howey = rand($starting_id, $ending_id);
// Now I use howey as my id
$SQL = "SELECT * FROM HIGHLIGHTS WHERE highlight_id = '$howey'";
$result = mysql_query( $SQL );
while( $row = mysql_fetch_array( $result ) ) {
$highlight_id = $row["highlight_id"];
$highlight_title = $row["highlight_title"];
}
echo "$highlight_id $highlight_title";
?>
----
Server IP: 69.147.83.197
Probable Submitter: 69.255.113.52
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/96378
Del: integrated -- https://master.php.net/note/delete/96378/integrated
Del: useless -- https://master.php.net/note/delete/96378/useless
Del: bad code -- https://master.php.net/note/delete/96378/bad+code
Del: spam -- https://master.php.net/note/delete/96378/spam
Del: non-english -- https://master.php.net/note/delete/96378/non-english
Del: in docs -- https://master.php.net/note/delete/96378/in+docs
Del: other reasons-- https://master.php.net/note/delete/96378
Reject -- https://master.php.net/note/reject/96378
Search -- https://master.php.net/manage/user-notes.php