centerwork Posted November 12, 2007 Share Posted November 12, 2007 I am try to create a script that select a random entry in my DB and Then check a variable in that entry. If that varible is set the script will select a new random number. The only probem is that it sometimes it selects the same random number as the last time it went through the loop. This would be fine except Sometimes the condition is never met, and no entry is selected. I also can't make the script run till one is select since it will be eventualy set to a cron job. (So I limited the max number of times the script runs to the number of entries in my DB.) Idealy a random function with a limiter something like this will be nice. $excluding = '1,6,8,9'; $rand = rand(1,10,"$excluding"); Unfortunatly this does not seem to be an option with rand() from the manual. What is the easyest way to do this? Quote Link to comment Share on other sites More sharing options...
Crew-Portal Posted November 12, 2007 Share Posted November 12, 2007 <?php $rand2 = rand(1,3); if ($rand2 == '1'){ $rand1 = rand(2,5); } elseif ($rand2 = '2'){ $rand1 = '7'; } elseif ($rand2 = '3'){ $rand1 = '10'; } echo $rand1; //This will Exho a random number between 1-10 But wont allow the numbers 1,6,8,9 ?> This is the only way I can think of! Quote Link to comment Share on other sites More sharing options...
centerwork Posted November 12, 2007 Author Share Posted November 12, 2007 No, That script return the same values. Results: 7 7 7 3 7 7 5 2 7 7 7 Would an array storing the values work? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 12, 2007 Share Posted November 12, 2007 The way i'd do it: Two tables in your database, one containing those numbers you don't want selected, and your table that you are trying to retrieve a random result from. Im suggeting using a table for the excluded numbers since i assume this will change. So, we have the table `excluded` containing a single column `number`. We then use the query: SELECT * FROM yourtable LEFT JOIN excluded ON excluded.number = yourtable.id WHERE excluded.number IS NULL ORDER BY RAND() LIMIT 1 Which selects one row, randomly ordered, where the id of our table is not in the excluded table. You would then update the excluded table with the id of the row retrieved. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 12, 2007 Share Posted November 12, 2007 try this <?php function urand($intMin,$intMax,$strExclude) { $intReturn = 0; $intFound = 0; $arrExclude = explode(",",$strExclude); while ($intTempRand = rand($intMin,$intMax)) { if (!in_array($intTempRand,$arrExclude)) { $intReturn = $intTempRand; $intFound=1; break; } } return $intReturn; } $excluding = '1,6,8,9'; $rand = urand(1,10,"$excluding"); echo $rand; ?> Quote Link to comment Share on other sites More sharing options...
Crew-Portal Posted November 12, 2007 Share Posted November 12, 2007 <?php $array = array ( array ( number => "1" ), array ( number => "2" ), array ( number => "3" ), array ( number => "4" ), array ( number => "5" ), array ( number => "7" ), array ( number => "10" ) ); $rand = rand(0,6); while (is_array($array[$rand]['number'])){ echo $array[$rand]['number']; ?> That should work using arrays? Quote Link to comment Share on other sites More sharing options...
Crew-Portal Posted November 12, 2007 Share Posted November 12, 2007 Wow 3 diffrent answers, all of which I believe will work! Thats one reason you gotta love PHP, Arrays, Functions, And Querys! Woo! Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 12, 2007 Share Posted November 12, 2007 modification to my function that one was too messy <?php function urand($intMin,$intMax,$strExclude) { $arrExclude = explode(",",$strExclude); while ($intTempRand = rand($intMin,$intMax)) { if (!in_array($intTempRand,$arrExclude)) { return $intTempRand; } } } $excluding = '1,6,8,9'; $rand = urand(1,10,"$excluding"); echo $rand; ?> Quote Link to comment Share on other sites More sharing options...
centerwork Posted November 12, 2007 Author Share Posted November 12, 2007 I never thought to use the mysql query to pick the random entry. <?php // Connects to your Database Here for($i=0; $i<=10; $i++) { $query = "SELECT * FROM visiter_print_log WHERE winner IS NULL ORDER BY RAND() LIMIT 1"; $results = mysql_query($query) or die (mysql_error()); $win_rows = mysql_num_rows($results); if($win_rows == '0') { echo 'No Winner.'; } $rows=mysql_fetch_array($results); $winner = $rows['entry_id']; //Picks a rand number that is not been selected aready. echo 'Winner: '.$winner.'<br />'; $query = "UPDATE visiter_print_log SET visiter_print_log.winner='1' WHERE visiter_print_log.entry_id = '$winner'"; $results = mysql_query($query) or die (mysql_error()); echo 'Results: '.$results.'<br />'; } ?> It worked GREAT... Thank you every one. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.