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
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
try changing $ticket = sort($ticket, SORT_NUMERIC); to just: sort($ticket, SORT_NUMERIC);
Again that made no change to the outcome, frustrating
frustrating indeed… lets cut it up and have some fun…
[php]$range = range(1, 25);
print_r($range);
echo “How does range look?
”;
$ticket = array();
for ($i=1; $i <= $n; $i++) {
$lotto = shuffle($range);
print_r($lotto);
echo “Do we have proper shuffle?
”;
foreach ($lotto as $value) {
if (count($ticket) != 4) {
if (!in_array($value, $ticket) {
$ticket[] = $value;
}
}
}
$print_r($ticket);
echo “are there 4 numbers?
”;
sort($ticket, SORT_NUMERIC);
$print_r($ticket);
echo “are there 4 numbers that are sorted?
”;
$que = (“INSERT INTO lotto
(id
,username
,one
,two
,three
,four
) VALUES (’’,’$usernameone’,’$lotton[0]’,’$lotton[1]’,’$lotton[2]’,’$lotton[3]’)”);
//mysql_query($que);
//echo "Ticket " . $i . ": " . $que . “
”; // test before putting in database
}[/php]
Right, after doing that theres certainly a problem or two.
Outcome:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 [10] => 11 [11] => 12 [12] => 13 [13] => 14 [14] => 15 [15] => 16 [16] => 17 [17] => 18 [18] => 19 [19] => 20 [20] => 21 [21] => 22 [22] => 23 [23] => 24 [24] => 25 ) How does range look?
1Do we have proper shuffle?
so shuffle was returning a boolean and killing the array we can fix that and continue:
[php]$lotto = range(1, 25);
$ticket = array();
for ($i=1; $i <= $n; $i++) {
shuffle($lotto);
print_r($lotto);
echo “Do we have proper shuffle?
”;
foreach ($lotto as $value) {
if (count($ticket) != 4) {
if (!in_array($value, $ticket) {
$ticket[] = $value;
}
}
}
$print_r($ticket);
echo “are there 4 numbers?
”;
sort($ticket, SORT_NUMERIC);
$print_r($ticket);
echo “are there 4 numbers that are sorted?
”;
$que = (“INSERT INTO lotto
(id
,username
,one
,two
,three
,four
) VALUES (’’,’$usernameone’,’$lotton[0]’,’$lotton[1]’,’$lotton[2]’,’$lotton[3]’)”);
//mysql_query($que);
//echo "Ticket " . $i . ": " . $que . “
”; // test before putting in database
}[/php]
That still returns a similar problem:
Array ( [0] => 11 [1] => 24 [2] => 15 [3] => 6 [4] => 12 [5] => 23 [6] => 16 [7] => 3 [8] => 14 [9] => 20 [10] => 9 [11] => 8 [12] => 22 [13] => 7 [14] => 21 [15] => 10 [16] => 13 [17] => 17 [18] => 19 [19] => 18 [20] => 5 [21] => 4 [22] => 1 [23] => 25 [24] => 2 ) Do we have proper shuffle?
looks good to me, what came after that? should have looked similar to:
Array( [0] => 11 [1] => 24 [2] => 15 [3] => 6 )are there 4 numbers?
Array( [0] => 6 [1] => 11 [2] => 15 [3] => 24 )are there 4 numbers that are sorted?
if so try :
[php]$lotto = range(1, 25);
$ticket = array();
for ($i=1; $i <= $n; $i++) {
shuffle($lotto);
foreach ($lotto as $value) {
if (count($ticket) != 4) {
if (!in_array($value, $ticket) {
$ticket[] = $value;
}
}
}
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]
Then if we’re looking good from there remove the // infront of the mysql_query and delete the last ‘echo’
–edit: I had to fix some variable names in the query that I had forgotten to change in my notepad
The outcome in the previous message is all that was there, the rest of the page was blank.
All though on using your latest code there was finally some value to it:
Ticket 1: INSERT INTO lotto
(id
,username
,one
,two
,three
,four
) VALUES (’’,‘Steven’,‘2’,‘6’,‘9’,‘24’)
Ticket 2: INSERT INTO lotto
(id
,username
,one
,two
,three
,four
) VALUES (’’,‘Steven’,‘2’,‘6’,‘9’,‘24’)
Ticket 3: INSERT INTO lotto
(id
,username
,one
,two
,three
,four
) VALUES (’’,‘Steven’,‘2’,‘6’,‘9’,‘24’)
Ticket 4: INSERT INTO lotto
(id
,username
,one
,two
,three
,four
) VALUES (’’,‘Steven’,‘2’,‘6’,‘9’,‘24’)
Ticket 5: INSERT INTO lotto
(id
,username
,one
,two
,three
,four
) VALUES (’’,‘Steven’,‘2’,‘6’,‘9’,‘24’)
Only problem being it was the same numbers for each one.
Just because this is frustrating and I wanted to have some fun with it…
[php]$lotto = range(1, 25);
$ticket = array();
for ($i=1; $i <= $n; $i++) {
shuffle($lotto);
for ($ii=1; $ii <= $i; $ii++;) {
shuffle($lotto); // shuffle the crap out of it
}
while(count($ticket) < 4) { // add a little extra randomness
$rand = array_rand($lotto, 1)
if (!in_array($rand, $ticket)) {
$ticket[] = $rand;
}
}
sort($ticket, SORT_NUMERIC); //sort it
$que = (“INSERT INTO lotto
(id
,username
,one
,two
,three
,four
) VALUES (’’,’$usernameone’,’$ticket[0]’,’$ticket[1]’,’$ticket[2]’,’$ticket[3]’)”);
//mysql_query($que); //insert it
echo "Ticket " . $i . ": " . $que . “
”; // test before putting in database
}[/php]