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.... Link to comment https://forums.phpfreaks.com/topic/228149-ordering-variables/ 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. Link to comment https://forums.phpfreaks.com/topic/228149-ordering-variables/#findComment-1176532 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? Link to comment https://forums.phpfreaks.com/topic/228149-ordering-variables/#findComment-1176692 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); Link to comment https://forums.phpfreaks.com/topic/228149-ordering-variables/#findComment-1176698 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 Link to comment https://forums.phpfreaks.com/topic/228149-ordering-variables/#findComment-1176699 Share on other sites More sharing options...
WilmotS Posted February 19, 2011 Author Share Posted February 19, 2011 Works great, thanks for your help. Link to comment https://forums.phpfreaks.com/topic/228149-ordering-variables/#findComment-1176700 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.