timmah1 Posted October 8, 2008 Share Posted October 8, 2008 I have a database with roughly 1800 stock symbols. Everyday I need to show only 50, but I need to have them be a different 50 everyday. So what I need to do is show a random 50 stock symbols on different days. I cannot just use random() because that would change everytime you went to that page. I need the same 50 to stay for the entire day. How would I go about doing this? Quote Link to comment https://forums.phpfreaks.com/topic/127538-simple-problem-i-think/ Share on other sites More sharing options...
Andy-H Posted October 8, 2008 Share Posted October 8, 2008 $n = 1800; $q = "SELECT stocks_used FROM table"; $r = mysql_query($q); $row = mysql_fetch_row($r); $arr = explode("-", $row[0]); for ($i = 0; $i < $n; $i++): $rand = rand(0,1800); if (!in_array($rand, $arr)): $arr[] = $rand; Endif; //Display stocks here Endfor; $new = implode("-", $arr); mysql_query("UPDATE table SET stocks_used = '$new' LIMIT 1"); /// stocks_used would be like 142-432-653-etc... Duno if you get the idea but I think that would work. Quote Link to comment https://forums.phpfreaks.com/topic/127538-simple-problem-i-think/#findComment-659854 Share on other sites More sharing options...
Orio Posted October 8, 2008 Share Posted October 8, 2008 I haven't tried this, but I think it should work. It's supposed to display $jumps stock symbols every day, each day showing the next group. <?php $jumps = 50; //How many stocks per day, can't be more than the number of stocks you have in your DB //First check how many stocks symbols you have $result = mysql_query("SELECT COUNT(*) FROM stocks_symbols"); list($num_symbols) = mysql_fetch_assoc($result); $start = (date('z') * $jumps) % $num_symbols; $end = $start + $jumps; $query = "(SELECT * FROM stocks_symbols ORDER BY id LIMIT {$start}, 50)"; if($end > $num_symbols-1) { $extra = $end - $num_symbols + 1; $query .= " UNION (SELECT * FROM stocks_symbols ORDER BY id LIMIT 0, {$extra})"; } $result = mysql_query($query); //All the data for today is inside $result, just loop through it ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/127538-simple-problem-i-think/#findComment-659865 Share on other sites More sharing options...
JonnoTheDev Posted October 8, 2008 Share Posted October 8, 2008 Use a seed with the mysql RAND() function. If you want them to change daily: $query = "SELECT stocks_used FROM table ORDER BY RAND(".date('d',time()).")"; Quote Link to comment https://forums.phpfreaks.com/topic/127538-simple-problem-i-think/#findComment-659869 Share on other sites More sharing options...
timmah1 Posted October 8, 2008 Author Share Posted October 8, 2008 Thank You Orio Quote Link to comment https://forums.phpfreaks.com/topic/127538-simple-problem-i-think/#findComment-659891 Share on other sites More sharing options...
timmah1 Posted October 8, 2008 Author Share Posted October 8, 2008 I just had a chance to test this code, but I am getting an error on this line Error Warning: Division by zero in /usr/home/thegr418/public_html/main/random.php on line 9 $start = (date('z') * $jumps) % $num_symbols; Quote Link to comment https://forums.phpfreaks.com/topic/127538-simple-problem-i-think/#findComment-660146 Share on other sites More sharing options...
Orio Posted October 9, 2008 Share Posted October 9, 2008 <?php //First check how many stocks symbols you have $result = mysql_query("SELECT COUNT(*) FROM stocks_symbols"); list($num_symbols) = mysql_fetch_assoc($result); ?> Have you changed the table's name from stocks_symbols into whatever it should be? If you did, try changing these two lines into: <?php //First check how many stocks symbols you have $result = mysql_query("SELECT COUNT(*) FROM stocks_symbols") or die(mysql_error()); list($num_symbols) = mysql_fetch_assoc($result) or die(mysql_error()); ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/127538-simple-problem-i-think/#findComment-661180 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.