seany123 Posted May 18, 2009 Share Posted May 18, 2009 i have a database with all my members details in... what im wanting to do is add a field called lottery... then when they enter... their lottery will = 1. then i want some way of picking a random members from my database who has lottery = 1 then... making a query with the selected member. Quote Link to comment Share on other sites More sharing options...
Masna Posted May 18, 2009 Share Posted May 18, 2009 I'm going to assume you know how to add a field called "lottery" and how to set it to '1' when a user chooses to enter the lottery. For selecting random lottery winners... $number_of_winners = 10; //change this to fit your needs $winners = array(); for($i = 0; $i < $number_of_winners; $i++){ $get_winner = mysql_query("SELECT * FROM `members` WHERE `lottery`='1' ORDER BY RAND() LIMIT 1"); $winners[] = mysql_fetch_assoc($get_winner); } //$winners now contains a multi-dimensional array of 10 winners Be sure to note that you may not actually have to query 10 winners separately, but possibly 10 winners in one query. I'm not sure exactly how ORDER BY RAND() works (whether it picks a random spot and then returns ordered rows from there or if it picks 10 rows independently and all at random). It probably randomizes each row selection. So, this may work... $number_of_winners = 10; $get_winners = mysql_query("SELECT * FROM `members` WHERE `lottery`='1' ORDER BY RAND() LIMIT ".$number_of_winners); //use $get_winners at your discretion But, again, I'm not sure and I'm too tired to test on my own. Quote Link to comment Share on other sites More sharing options...
seany123 Posted May 18, 2009 Author Share Posted May 18, 2009 I'm going to assume you know how to add a field called "lottery" and how to set it to '1' when a user chooses to enter the lottery. For selecting random lottery winners... $number_of_winners = 10; //change this to fit your needs $winners = array(); for($i = 0; $i < $number_of_winners; $i++){ $get_winner = mysql_query("SELECT * FROM `members` WHERE `lottery`='1' ORDER BY RAND() LIMIT 1"); $winners[] = mysql_fetch_assoc($get_winner); } //$winners now contains a multi-dimensional array of 10 winners Be sure to note that you may not actually have to query 10 winners separately, but possibly 10 winners in one query. I'm not sure exactly how ORDER BY RAND() works (whether it picks a random spot and then returns ordered rows from there or if it picks 10 rows independently and all at random). It probably randomizes each row selection. So, this may work... $number_of_winners = 10; $get_winners = mysql_query("SELECT * FROM `members` WHERE `lottery`='1' ORDER BY RAND() LIMIT ".$number_of_winners); //use $get_winners at your discretion But, again, I'm not sure and I'm too tired to test on my own. yeah i think i understand what to do now... i only want 1 winner so i just set it to 1 instead or remove it completely? Quote Link to comment Share on other sites More sharing options...
seany123 Posted May 18, 2009 Author Share Posted May 18, 2009 I tried to echo get winners and it didnt echo anything... hmm Quote Link to comment Share on other sites More sharing options...
seany123 Posted May 18, 2009 Author Share Posted May 18, 2009 ive tried the above method and i cannot get it to work. Quote Link to comment Share on other sites More sharing options...
Masna Posted May 18, 2009 Share Posted May 18, 2009 I tried to echo get winners and it didnt echo anything... hmm Lol, well of course not. It's a result resource link. You have to use mysql_fetch_assoc() to get a usable array from the SELECT query. $number_of_winners = 1; $get_winners = mysql_query("SELECT * FROM `members` WHERE `lottery`='1' ORDER BY RAND() LIMIT ".$number_of_winners); $winner = mysql_fetch_assoc($get_winners); //$winner now stores an array of data for one particular (randomly chosen) member //Assuming that members have a username... echo $winner['username']; Quote Link to comment Share on other sites More sharing options...
seany123 Posted May 18, 2009 Author Share Posted May 18, 2009 I tried to echo get winners and it didnt echo anything... hmm Lol, well of course not. It's a result resource link. You have to use mysql_fetch_assoc() to get a usable array from the SELECT query. $number_of_winners = 1; $get_winners = mysql_query("SELECT * FROM `members` WHERE `lottery`='1' ORDER BY RAND() LIMIT ".$number_of_winners); $winner = mysql_fetch_assoc($get_winners); //$winner now stores an array of data for one particular (randomly chosen) member //Assuming that members have a username... echo $winner['username']; ah yes good point... tried that and worked good thanks Quote Link to comment Share on other sites More sharing options...
seany123 Posted May 18, 2009 Author Share Posted May 18, 2009 just wondering... how would i then be able to count the amount of members with lottery == 1? Quote Link to comment Share on other sites More sharing options...
Masna Posted May 18, 2009 Share Posted May 18, 2009 $count_q = mysql_query("SELECT COUNT(lottery) FROM `members` WHERE `lottery`='1'"); $count_assoc = mysql_fetch_assoc($count_q); $amount_of_members_with_lottery_equal_equal_1 = $count_assoc['COUNT(lottery)']; 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.