azpaul Posted May 13, 2008 Share Posted May 13, 2008 Hi, I am fairly new to PHP and I am able to stumble through most of what I need to do with the help of these forums and PHP.net. There are a lot of great support bulletins that cover a lot of issues. However, I have not been able to find one to help me with my issue. I am designing a Keno Game. I have a PHP script running in cron that generates a flat file database with 20 random numbers every X minutes. I have designed a PHP page that a person can enter in the Game Number and their picks up to 10 numbers using a post method. I am able to get all the information passed from the search page to the results page with no problem. I am able to read in the flat file and find the row with the proper game number and assign the 20 numbers into an array for criteria matching. I am able to display all the information to the screen correctly. Everything to this point is working perfect, although I am sure the code could be written cleaner. The part I am having a problem is counting the number of hits the player selected to that in the random number draw from the computer. I use a foreach loop to compare each number in the array to that number that was selected by the player. The problem is that the very last number is not checked in the loop or is ignored. I can echo the variables and they print correctly on the screen. Now all the other numbers in the array get check and pass the criteria if true. It is just that last number of the array that it breaks on. I can not see anywhere why this would not be checked by the if statement. The 20 number data set I am using is 63, 68, 64, 66, 14, 48, 25, 8, 67, 33, 6, 69, 36, 18, 16, 23, 24, 38, 57, 31 The Variables $PickOne through $PickTen is the data entered from the search screen for the numbers the player picked. The Variable $drawn is read in from another section from the flat file and is then made into an array with explode. 4 Numbers used in this order to test for matches 36 25 31 45 PickThree is the third number checked but could easily be PickFour or PickFive respectively. This is the last number in t he array, 31. Any help would greatly be appreciated. Thanks!! Echo Test Results PickThree = 31 Number = 1 Value = 63 Number = 2 Value = 68 Number = 3 Value = 64 Number = 4 Value = 66 Number = 5 Value = 14 Number = 6 Value = 48 Number = 7 Value = 25 25 Matched Second Spot Number = 8 Value = 8 Number = 9 Value = 67 Number = 10 Value = 33 Number = 11 Value = 6 Number = 12 Value = 69 Number = 13 Value = 36 36 Match 1st Spot Number = 14 Value = 18 Number = 15 Value = 16 Number = 16 Value = 23 Number = 17 Value = 24 Number = 18 Value = 38 Number = 19 Value = 57 Number = 20 Value = 31 Total Hits 2 Here is the snippet of code. $number = 0; // used to echo the array number $counter = 0; echo "PickThree = $PickThree<br>"; foreach ($drawn as &$value) { $number++; echo "Number = $number "; echo "Value = $value<br>"; if ($value == $PickOne) { echo "$value Match 1st Spot<br>"; $counter++; } elseif ($value == $PickTwo) { echo "$value Matched Second Spot<br>"; $counter++; } elseif ($value == $PickThree) { echo "$value Matched Third Spot<br>"; $counter++; } elseif ($value == $PickFour) { echo "$value Matched Fourth Spot<br>"; $counter++; } elseif ($value == $PickFive) { echo "$value Matched Fifth Spot<br>"; $counter++; } elseif ($value == $PickSix) { echo "$value Matched Sixth Spot<br>"; $counter++; } elseif ($value == $PickSeven) { echo "$value Matched Seventh Spot<br>"; $counter++; } elseif ($value == $PickEight) { echo "$value Matched Eighth Spot<br>"; $counter++; } elseif ($value == $PickNine) { echo "$value Matched Nineth Spot<br>"; $counter++; } elseif ($value == $PickTen) { echo "$value Matched Tenth Spot<br>"; $counter++; } } echo "Total Hits $counter<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/105370-solved-foreach-matching-criteria-ignors-last-value-in-if-elseif/ Share on other sites More sharing options...
corbin Posted May 13, 2008 Share Posted May 13, 2008 So.... $drawn are the numbers given by the user? And.... The cron script sets up 20 random numbers and then the script later grabs 10 of those random numbers and compares $drawn to them? So... $PickOne, $PickTwo so on are the 10 random values from the file? I'm confused. Is that what you're trying to do? If so, your code should be working correctly. Can I see the code where $drawn and $Pick<num> are set? Now that I've tried to help you with your code, I would like to present an easier (well... to read anyway ;p) alternative. Let's say you have the file numbers.txt with the 20 numbers in the format "a,b,c,d,e,f...." Now, let's do this more array oriented. <?php $max_guesses = 10; $showform = true; if($_POST) { //user has sumitted their numbers.... continue with processing numbers $guesses = (isset($_POST['guess'])) ? $_POST['guess'] : array(); //$guesses now holds the users gueses. if(count($guesses) == $max_guesses) { //the number of guesses matches the max number. //Note, you would want to do this better. For example, I don't check for repeated guesses or if a guess is simply blank. $numbers = file('numbers.txt'); $numbers = explode(',', $numbers); //narrow numbers down to 10... shuffle($numbers); //randomize the order of the numbers $tmp = $numbers; //hold the old numbers in $tmp $numbers = array(); //make a new numbers array that is empty for($i = 0; $i < $max_guesses; $i++) { $numbers[] = $tmp[$i]; } $tmp = null; //get rid of the old numbers $correct = array(); //array to hold correct guesses foreach($guesses as $guess) { if(in_array($guess, $numbers)) { $correct[] = $guess; } } $correct_count = count($correct); if($correct_count > 0) { echo "You got the following guesses correct:" . PHP_EOL; foreach($correct as $c) { echo $c . '<br />' . PHP_EOL; } } else { echo "Unfortunately, none of your guesses were correct."; } } else { echo '<font color="red">Incorrect number of guesses!</font><br /><br />' . PHP_EOL; } } if(!$showform) { //user has not submitted their numbers or their was an error; show the form //I'm so lazy I'm going to even generate the form. echo '<form action="" method="POST">' . PHP_EOL; //open the form for($i = 1; $i <= $max_guesses; $i++) { //note that I set it to 1 rather than 0 so I can print it out and it make sense without having to add 1 echo "Number {$i}: " . '<input type="text" name="guess[]" value="" maxlength="2" />' . PHP_EOL; } echo '<br><input type="submit" value="Submit!" />' . PHP_EOL; echo '</form>' . PHP_EOL; //close the form } ?> That's quite sparsely documented, so if you have any questions of what is going on, please ask. Quote Link to comment https://forums.phpfreaks.com/topic/105370-solved-foreach-matching-criteria-ignors-last-value-in-if-elseif/#findComment-539666 Share on other sites More sharing options...
azpaul Posted May 13, 2008 Author Share Posted May 13, 2008 Thanks for the input Corbin. $drawn is actually being read in from a text file. This file has the format of Game: 0> 67, 21, 74, 45, 19, 54, 41, 13, 29, 34, 73, 3, 63, 7, 22, 53, 49, 57, 36, 76 Game: 1> 31, 68, 56, 19, 28, 51, 2, 69, 6, 39, 7, 54, 48, 1, 46, 60, 75, 13, 62, 32 Game: 2> 60, 17, 68, 42, 46, 21, 22, 65, 15, 77, 32, 27, 62, 63, 58, 30, 56, 43, 34, 72 Game: 3> 11, 59, 60, 38, 48, 73, 32, 53, 25, 43, 68, 79, 62, 76, 80, 64, 45, 36, 49, 23 Game: 4> 69, 9, 20, 76, 54, 23, 41, 66, 59, 71, 29, 21, 38, 15, 46, 19, 5, 52, 31, 39 Game: 5> 33, 16, 50, 24, 71, 25, 58, 70, 53, 10, 13, 15, 4, 74, 30, 80, 9, 68, 54, 59 Game: 6> 62, 53, 28, 32, 60, 59, 10, 15, 65, 74, 42, 22, 8, 66, 48, 17, 11, 23, 37, 30 Game: 7> 51, 25, 18, 13, 20, 67, 27, 4, 26, 68, 54, 10, 55, 28, 66, 53, 52, 64, 78, 63 A search page allows the user to enter 2 - 10 numbers ranging from 1-80. These user numbers are assigned to $PickOne thru $PickTen. You can see a the development pages at www.101casinoparties.com/keno/keno_search.php. I like the idea of using all arrays but I am not knowledgeable enough to follow your script at this time. I am sorry. I have a lot to learn. Here is the entire script so far. Maybe this will help with understanding my logic. <?php $submit = $_POST['submit']; $GameNumber = $_POST['GameNumber']; $GameNumberSearch = "Game: ".$GameNumber; $PickOne = $_POST['PickOne']; $PickTwo = $_POST['PickTwo']; $PickThree = $_POST['PickThree']; $PickFour = $_POST['PickFour']; $PickFive = $_POST['PickFive']; $PickSix = $_POST['PickSix']; $PickSeven = $_POST['PickSeven']; $PickEight = $_POST['PickEight']; $PickNine = $_POST['PickNine']; $PickTen = $_POST['PickTen']; $Stake = $_POST['Stake'] ?> <?php $drawone = 12; $drawtwo = 45; $drawthree = 79; $drawnarray = array($drawone,$drawtwo,$drawthree); $comma_separated = implode(",", $drawnarray); //echo "Searching For $GameNumber <br>"; //Open File for Reading if(!$fp = fopen("gamedraw.txt","r")) { echo "cannot open the file"; } else { //while the file is not empty //echo "Passed Failure. Testing Further\n"; $i = 0; $counter = 0; while(!feof($fp)) { //read a line at a time to try and find a match $read_line = fgets($fp,255); $pieces = explode(">", $read_line); //echo "Game Variable $pieces[0]<br>"; if ($pieces[0] == $GameNumberSearch) $matched = "YES"; if ($pieces[0] == $GameNumberSearch) $game = $pieces[1]; $drawn = explode(",", $game); } if ($matched != "YES") echo "$GameNumberSearch --> NO SUCH GAME!<br>"; fclose($fp); } $number = 0; $counter = 0; echo "PickThree = $PickThree<br>"; foreach ($drawn as &$value) { $number++; echo "Number = $number "; echo "Value = $value<br>"; if ($value == $PickOne) { echo "$value Match 1st Spot<br>"; $counter++; } elseif ($value == $PickTwo) { echo "$value Matched Second Spot<br>"; $counter++; } elseif ($value == $PickThree) { echo "$value Matched Third Spot<br>"; $counter++; } elseif ($value == $PickFour) { echo "$value Matched Fourth Spot<br>"; $counter++; } elseif ($value == $PickFive) { echo "$value Matched Fifth Spot<br>"; $counter++; } elseif ($value == $PickSix) { echo "$value Matched Sixth Spot<br>"; $counter++; } elseif ($value == $PickSeven) { echo "$value Matched Seventh Spot<br>"; $counter++; } elseif ($value == $PickEight) { echo "$value Matched Eighth Spot<br>"; $counter++; } elseif ($value == $PickNine) { echo "$value Matched Nineth Spot<br>"; $counter++; } elseif ($value == $PickTen) { echo "$value Matched Tenth Spot<br>"; $counter++; } } echo "Total Hits $counter<br>"; //echo $drawn[0] .$drawn[1]. $drawn[2]. $drawn[3]. $drawn[4]. $drawn[5]. $drawn[6] //. $drawn[7]. $drawn[8]. $drawn[9]. $drawn[10]. $drawn[11]. $drawn[12]. $drawn[13] //. $drawn[14]. $drawn[15]. $drawn[16]. $drawn[17]. $drawn[18]. $drawn[19]. $drawn[20]; //$i=0; //$counter=0; // if ($drawn[0] == $PickOne Or $drawn[0] == $PickTwo Or $drawn[0] == $PickThree ) // echo "$drawn[0] is a HIT"; // else echo "No HITS"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/105370-solved-foreach-matching-criteria-ignors-last-value-in-if-elseif/#findComment-539676 Share on other sites More sharing options...
azpaul Posted May 13, 2008 Author Share Posted May 13, 2008 I was able to get the last variable in to validate in an if statement. Thank you Corbin for giving me the idea to use all arrays... I was able to set all parameters up with a nested foreach using 1 if statement. Much more efficient and easier to read!! But, in order for the last variable in the data set to be read, I have to add another , delimiter??? So now there is really another empty field at the end make it 21 places instead of 20. That is what I do not understand. Any how, it is working and thanks again Corbin for the new thought! Here is the much cleaner code... I have the echos to see what was going on. I will be removing them... $number = 0; $counter = 0; //Compare Game Numbers with Ticket Numbers Using arrays. foreach ($drawn as $value) { $number++; $spot= 0; echo "Drawn Number = $number "; echo "Value = $value<br>"; //$value is that actual number for comparison and drawn number is the Nth number. //Ticket Array Numbers foreach ($ticketarray as $ticket) { $spot++; if ($ticket == $value) { $counter++; echo "HIT "; } else { echo "NO HIT "; } echo "SPOT $spot $ticket<br>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/105370-solved-foreach-matching-criteria-ignors-last-value-in-if-elseif/#findComment-539709 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.