WilmotS Posted February 18, 2011 Share Posted February 18, 2011 I have a lottery PHP script which i have been creating. The lottery contains four different numbers in which are four different fields in my database. On the random ticket buy the code for the four numbers are like this $ticketone = rand(1,25); $tickettwo = rand(1,25); $ticketthree = rand(1,25); $ticketfour = rand(1,25); I am wanting to do it so they can be put in order and then imported into my database in that right order. For example, If ticketone was 20, an tickettwo was 13, i would want it to be imported into the database so that ticketone would be 13. (being put into order) and ticket two would be 20 etc.... Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2011 Share Posted February 18, 2011 Use an array to store the numbers, then sort the array. Quote Link to comment Share on other sites More sharing options...
WilmotS Posted February 19, 2011 Author Share Posted February 19, 2011 My code currently stands like this $ticketone = rand(1,25); $tickettwo = rand(1,25); $ticketthree = rand(1,25); $ticketfour = rand(1,25); function lotterytickets($ticketone,$tickettwo,$ticketthree,$ticketfour){ $array = array("$ticketone","$tickettwo","$ticketthree","$ticketfour"); sort($array,SORT_NUMERIC); $o = $array[0]; $t = $array[1]; $th = $array[2]; $f = $array[3]; $last = $o.$t.$th.$f; return $last; } mysql_query("INSERT INTO `lotto` (`id`,`username`,`one`,`two`,`three`,`four`) VALUES ('','$username','$o','$t','$th','$f')"); Only where it insters into Lotto the numbers are all shown as 0. Meaning something isnt functioning right. Wheres the problem? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 19, 2011 Share Posted February 19, 2011 The function works, but I don't see where you ever call it. However, the variables $o, $t, $th, and $f don't exist outside of the function. I think I'd change the function to let it do all the work and just return an array, then use the elements in the query. function lotterytickets(){ $tick = array(); $tick[] = rand(1,25); $tick[] = rand(1,25); $tick[] = rand(1,25); $tick[] = rand(1,25); sort($tick, SORT_NUMERIC); return $tick; } $tickets = lotterytickets(); $query = "INSERT INTO `lotto` (`username`, `one`, `two`, `three`, `four`) VALUES ('$username', $tickets[0], $tickets[1], $tickets[2], $tickets[3])"; mysql_query($query); Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 19, 2011 Share Posted February 19, 2011 You have a function, which you're not calling. The variables inside the function are not available outside the function. Try something like this: <?php $ary = array(rand(1,25),rand(1,25),rand(1,25),rand(1,25)); sort($ary); $q = "insert into lotto (`id`,`username`,`one`,`two`,`three`,`four`) VALUES ('','$username','{$ary[0]}','{$ary[1]}','{$ary[2]}','{$ary[3]}'"; $rs = mysql_query($q); ?> You don't need the function. Ken Quote Link to comment Share on other sites More sharing options...
WilmotS Posted February 19, 2011 Author Share Posted February 19, 2011 Works great, thanks for your help. 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.