While Lopping

I have a piece of code below for my lottery system,
I am trying to do a code that allows the user to buy X amount of tickets rather than one individually each time.
Only with the code i have the page just crashed and goes blank when doing it.
Here is the code used:

<? $doit = $_POST['xamount']; if($_POST['xtickets'] AND $doit > 0){ $buycar = $doit; $n = $buycar; $i = 0; if($n >= 1){ while ($i < $n){ $doit = $buycar[$i]; $possibles = array("1","2","3","4","5","6","7","8","9","10","11","12","12","14","15","16","17","18","19","20","21","22","23","24","25"); shuffle($possibles); $ticketone = $possibles[0]; $tickettwo = $possibles[1]; $ticketthree = $possibles[2]; $ticketfour = $possibles[3]; function cockmunch($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; } $list = cockmunch($ticketone,$tickettwo,$ticketthree,$ticketfour); $listk = explode("-",$list); $yourone = $listk[0]; $yourtwo = $listk[1]; $yourthree = $listk[2]; $yourfour = $listk[3]; } $i = $i + 1; mysql_query("INSERT INTO `lotto` (`id`,`username`,`one`,`two`,`three`,`four`) VALUES ('','$usernameone','$yourone','$yourtwo','$yourthree','$yourfour')"); }} ?> Buy X amount of tickets:

Where is the problem that wont do all the tickets?

Looks like you have an infinite loop… $i++; needs to be performed before you close the while loop. Should also move the function out of the loop.

Also take a look at the rand() function, rather than your array you can pull numbers with just rand(1, 25); or rand(1, $end); where you can place $end at top of page or in a CFO file so you can change that number either through an admin panel on the site or manually in the file easier

I have changed it so it does $i++; before closing the loop but it has not made a difference.
Moving the function out of the loop dont generate the numbers properly.
I cant use the rand() function as it duplicates numbers and I also need to order the numbers for smallest to biggest.
Each time i run the post it is only inserting 1 row of tickets, not the number of tickets im trying to buy.

how many numbers are on a ‘ticket’?

Four different numbers from 1 - 25

Try this, shortened and simplified a bit

[php]$doit = $_POST[‘xamount’];
if($_POST[‘xtickets’] AND $doit > 0){
$buycar = $doit;
$n = $buycar;
$que = (“INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,’$usernameone’,’ “);
if($n >= 1){
for ($i=1; $i <= $n; $i++) {
$lotto = shuffle(range(1,25));
//I would try following line as while I havn’t used it in a long time I believe it should work
//$tickets = implode(”-”, sort(array_rand($lotto, 4), SORT_NUMERIC);

		//otherwise use the following and delete "[$i]" from $tickets beneath it
		$tickets =  $lotto[1] . "-" . $lotto[2] . "-" . $lotto[3] . "-" . $lotto[4];
		if ($i == $n) {
			$que = $que . $tickets[$i] . "')";
		} else {
			$que = $que . $tickets[$i] . "', ";
		}
	}
	//mysql_query($que);
	echo $que; // test before putting in database
}

}[/php]

Or this, I think is closer to what you’re actually trying to do, as I was putting each ticket in the same query rather than creating a query for each ticket.

[php]$doit = $_POST[‘xamount’];
if($_POST[‘xtickets’] AND $doit > 0){
$buycar = $doit;
$n = $buycar;
$que = (“INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,’$usernameone’,’ “);
if($n >= 1){
for ($i=1; $i <= $n; $i++) {
$lotto = shuffle(range(1,25));
$lotton = $lotto[1] .”-”. $lotto[2] ."-". $lotto[3] ."-". $lotto[4];
$lotton = sort(explode("-", $lotton), SORT_NUMERIC);
$que = (“INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,’$usernameone’,’$lotton[1]’,’$lotton[2]’,’$lotton[3]’,’$lotton[4]’)”);
//mysql_query($que);
echo "Ticket " . $i . ": " . $que . “
”; // test before putting in database
}
}
}[/php]

You can use your old code I believe, but like the $i++; that you moved inside the loop, you’d have to move your query inside the loop as well. that’s why you’re only getting the first ticket. Sorry to over complicate I misread a lot.

Worked a treat, thankyou.

Done the wrong thing there,
When using the code you just wrote i got

Ticket 1: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)
Ticket 2: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)
Ticket 3: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)
Ticket 4: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)
Ticket 5: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)

It dont actually insert the query into the database?

Hm, no he commented out the mysql_action and put the result in an echo.

[php]
//mysql_query($que);
echo "Ticket " . $i . ": " . $que . “
”; // test before putting in database
[/php]

He even said why. :stuck_out_tongue:

Good luck.
O.

Yes realised that in the end, I was meant to mention the fact that none of the numbers actually generate as you can see from the queries echoed for the testing.

yup just change to:

mysql_query($que);
//echo "Ticket " . $i . ": " . $que . “
”; // test before putting in database

more importantly there are no number values for the ticket and I left a little mess :slight_smile:
so lets find out why with:

[php]$doit = $_POST[‘xamount’];
if($_POST[‘xtickets’] AND $doit > 0){
$buycar = $doit;
$n = $buycar;
if($n >= 1){
for ($i=1; $i <= $n; $i++) {
$lotto = shuffle(range(1,25));
echo “Shuffled:”
print_r($lotto);
echo “

”;
$lotton = $lotto[1] ."-". $lotto[2] ."-". $lotto[3] ."-". $lotto[4];
echo “ticket:”;
print_r($lotton);
echo “

”;
$lotton = sort(explode("-", $lotton), SORT_NUMERIC);
echo “sorted:”;
print_r($lotton);
echo “

”;
$que = (“INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,’$usernameone’,’$lotton[1]’,’$lotton[2]’,’$lotton[3]’,’$lotton[4]’)”);
//mysql_query($que);
echo "Ticket " . $i . ": " . $que . “
”; // test before putting in database
}
}
}[/php]

With that coding it turnt out with the same outcome:

Shuffled:1

ticket:—

sorted:1

Ticket 1: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)
Shuffled:1

ticket:—

sorted:1

Ticket 2: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)
Shuffled:1

ticket:—

sorted:1

Ticket 3: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)
Shuffled:1

ticket:—

sorted:1

Ticket 4: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)
Shuffled:1

ticket:—

sorted:1

Ticket 5: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)

Still no number values

Where exactly are $lotto[1…4] getting filled?
I see them getting shuffled…

Am I missing something?
O.

[php]$que = (“INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,’$usernameone’,’$lotton[1]’,’$lotton[2]’,’$lotton[3]’,’$lotton[4]’)”);
//mysql_query($que);
echo "Ticket " . $i . ": " . $que . “
”; // test before putting in database[/php]

The $lotton is being filled into the “$que” query, if thats what your talking about?

No I’m talking about:
[php]
for ( $i=1; $i<=4; $i++ )
{ $lotto[$i] = rand( 1, 25 );
}
[/php]

O.

try this, much cleaner anyway

[php]$range = range(1, 25);
$ticket = array();
for ($i=1; $i <= $n; $i++) {
$lotto = shuffle($range);
foreach ($lotto as $value) {
if (count($ticket) != 4) {
if (!in_array($value, $ticket) {
$ticket[] = $value;
}
}
}
$ticket = sort($ticket, SORT_NUMERIC);
$que = (“INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,’$usernameone’,’$ticket[0]’,’$ticket[1]’,’$ticket[2]’,’$ticket[3]’)”);
//mysql_query($que);
echo "Ticket " . $i . ": " . $que . “
”; // test before putting in database
}[/php]

Again that PHP code is coming up with the same result as previous:
Ticket 1: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)
Ticket 2: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)
Ticket 3: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)
Ticket 4: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)
Ticket 5: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,’’,’’,’’,’’)

Not generating any numbers

weird… try changing $range to:

$range = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25);

Maybe the range() function isn’t working for whatever reason?
or do a print_r($ticket); after the sort

Changing the Range made no difference to the outcome, using the print_r($ticket); just showed the number 1 hmm

Sponsor our Newsletter | Privacy Policy | Terms of Service