Hi i am making a new site, and i need someone helping me build a script that, could randomly appear images, and so you can vote on the hottest picture, quite simmalare to Hotornor.com! Could rly need some help
You could always just go to the next image in the line as to not repeat images with the mysql approach. Simply use RND to start the sequence at a random spot in the archive but rotate the images in the databases set order.
You shouldn't use ORDER BY RAND(). It's very inefficient, it re-queries the data and assigns an ID and then displays the data.
Yeah and i need a voting system! so if its random the votes would not get saved. So i need another one.
$Data = mysql_query("SELECT `ID`, `Image` FROM Table", $ConnectionID) or die(mysql_error()); $Count = mysql_num_rows($Data); if($Count > 0) { $ID = array(); while($Fetch = mysql_fetch_assoc($Data)) { $ID[] = $Fetch['ID'] } $Select = mysql_fetch_assoc(mysql_query("SELECT `ID`, `Image` FROM Table WHERE `ID` = ".$ID[rand(0, (count($ID)-1))]."", $ConnectionID)); $Image = $Select['Image']; } There, random image....
@W3Theory, even your method will get slow if the table contains thousands of image. Looping through each image for creating an array will take time, more over, it may as well overload server in terms of memory and processor utilization. Ideal way would be: 1. Make a query to select minimum and maximum id from table. SELECT MIN(id), MAX(id) FROM table_name 2. Using PHP generate a random number between min and max id. 3. Make a query to select one row present at that id or greater. SELECT COLUMN_NAMES FROM table_name WHERE id >= random_id This would make it very fast. Note: Above logic is based on assumption that table contains a unique column id or similar.