hamburgerlove413 Posted October 7, 2013 Share Posted October 7, 2013 (edited) Hello, I'm trying to create a simple phrase guessing game, and I have a button named Next Phrase which when clicked, I need to increment a variable, $nextPhrase by 1. I have it kind of working, but for some reason on the first time, it takes two clicks to get to start incrementing. Can anyone see what it is I'm doing wrong? <?php session_start(); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Project 2</title> <link href="css/layout.css" rel="stylesheet" type="text/css"> </head> <body> <div id="container" class="clearfix"> <?php print "<form method='post'>"; function inputscreen($guesses) { print "<span>Guess a letter!</span>"; print "<span> You have ". $guesses . " guesses left! </span>"; print "<input type='text' name='guess' />"; print "<input type='submit' name='action' value='GUESS' />"; print "<input type='submit' name='action' value='REVEAL' />"; print "<input type='submit' name='action' value='NEXT PHRASE'>"; print "</form>"; } function printTable($letterCount, $visibleArray, $wordArray) { // print table for word display print "\n \n <table> \n \n \t <tr>"; for ($i = 0; $i < $letterCount; $i++) { if ($visibleArray[$i] == 1) { print "\n \n \t \t <td>" .$wordArray[$i]. "</td> \n"; } else if ($wordArray[$i] == " ") { print "\n \n \t </tr> \n \n</table> \n \n <table> \n \n \t <tr>"; } else { print "\n \n \t \t <td> </td>"; } } print "\n \n \t </tr> \n \n </table> \n \n"; } //text loader function textLoader($whatfile) { if(!file_exists($whatfile)) { print "Sorry, can't find " . $whatfile; exit; } else { $textarray= file($whatfile); return $textarray; } } // global variables if (!isset($_SESSION['nextPhrase'])) { $_SESSION['nextPhrase'] = 0; } $nextPhrase = $_SESSION['nextPhrase']; print $nextPhrase . "<br />"; $theText = textLoader('words.txt'); $action = $_POST['action']; $team = $_SESSION['team']; $visibleArray = $_SESSION['visible']; $guesses = $_SESSION['guesses']; $word = $theText[$nextPhrase]; $wordArray = str_split($word); $letterCount = count($wordArray)-2; if (!$action) { $guesses = 5; for ($i = 0; $i < $letterCount; $i++) { $visibleArray[$i] = 0; } inputscreen($guesses); printTable($letterCount, $visibleArray, $wordArray); } else if ($action == "GUESS") { $guess = strtoupper($_POST['guess']); if (preg_match("/^[a-zA-Z]$/", $guess)) { if (in_array($guess, $wordArray)) { for ($i=0; $i < $letterCount; $i++) { if ($guess == $wordArray[$i]) { $visibleArray[$i] = 1; } } print "<span>You guessed the letter " .$guess . " and are correct! </span>"; } else { print "<span>Nope! Try Again!</span> "; $guesses -= 1; } } else { print "<span>You need to guess a letter!</span>"; } inputscreen($guesses); printTable($letterCount, $visibleArray, $wordArray); } else if ($action == "REVEAL") { for ($i =0; $i < $letterCount; $i++) { if ($wordArray[$i] != " ") { $visibleArray[$i] = 1; } } print "<div>The phrase was </div>"; printTable($letterCount, $visibleArray, $wordArray); print "<input type='submit' name='action' value='NEXT PHRASE'>"; } else if ($action == "NEXT PHRASE") { for ($i = 0; $i < $letterCount; $i++) { $visibleArray[$i] = 0; } inputscreen($guesses); printTable($letterCount, $visibleArray, $wordArray); $nextPhrase++; } $_SESSION['nextPhrase'] = $nextPhrase; $_SESSION['visible'] = $visibleArray; $_SESSION['team'] = $team; $_SESSION['guesses'] = $guesses; ?> </div> </body> </html> Edited October 7, 2013 by hamburgerlove413 Quote Link to comment Share on other sites More sharing options...
requinix Posted October 7, 2013 Share Posted October 7, 2013 You output the phrase before you increment. At the end of the script you've incremented but it's too late to change the message you were showing. Quote Link to comment Share on other sites More sharing options...
hamburgerlove413 Posted October 7, 2013 Author Share Posted October 7, 2013 I tried moving $nextPhrase++ above inputscreen($guesses); printTable($letterCount, $visibleArray, $wordArray); but it didn't change anything. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 7, 2013 Share Posted October 7, 2013 When the "NEXT PHRASE" button is clicked, you don't actually update the phrase counter until the end of the script. The script, however, needs $nextPhrase to be updated prior to this line: $word = $theText[$nextPhrase]; Quote Link to comment Share on other sites More sharing options...
hamburgerlove413 Posted October 8, 2013 Author Share Posted October 8, 2013 (edited) Is there a better way to approach this then? I'm having other issues besides it taking two clicks, like after I click the next phrase button and it finally changes, the guess button then changes the phrase again the first time it's clicked. It doesn't do this unless the next phrase button is first clicked. Edited October 8, 2013 by hamburgerlove413 Quote Link to comment Share on other sites More sharing options...
hamburgerlove413 Posted October 8, 2013 Author Share Posted October 8, 2013 I should say I'm not so much looking for a written out solution as much as what I should look into to make it do what I need it to do. Or if I'm at least on the right track, where I can go from here to make it work. Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted October 8, 2013 Solution Share Posted October 8, 2013 input-processing-output this is the sequence/flow that all programming should follow. you can apply this methodology to just one line of code, a block of related code, or the entire program. whatever context you are looking at, it will have some definable input(s), processing, and output(s). for any step in your logic, you need to condition, filter, and validate the relevant input(s) values (the input may be just the fact that your code was requested/executed and is not an actual value.) then, if the input(s) values are valid and error free, you can use those input values and move onto the processing you need for any step in your logic. finally, produce any output the step in your logic is responsible for. this can be output that is displayed or data that is used in the following steps in your program. by doing this last, for any step in your logic, you insure that any changes due to the inputs will be present in the output. now for your current problem. the main functional body of your php code expects, as an input to it, a Phrase Number to access the correct line in the 'words.txt' file. you would also like to be able to increment the Phrase Number when $action == "NEXT PHRASE". the Phrase Number should also initially start at zero and it should be limited to the line count ( minus 1 ) of the 'words.txt' file. an example of how you might do this (this code is using the $_SESSION[..] variable directly, rather than coping to/from another php variable, and renames this session variable to indicate what the value in it means, it's the current phrase the code is using, not the next one) - if(!isset($_SESSION['PhraseNumber'])){ $_SESSION['PhraseNumber'] = 0; // initialize to zero (the first line in the file) } $action = $_POST['action']; // this input needs to be conditioned/filtered/validated as appropriate if($action == "NEXT PHRASE"){ $_SESSION['PhraseNumber']++; // increment to the next phrase in the file $action = ''; // start at the first step in the main logic below } $theText = textLoader('words.txt'); // you can put logic here to test count($theText) and $_SESSION['PhraseNumber'] to detect when all phrases have been used. the $action variable is being cleared (in real life you would redirect back to the same page to clear any $_POST data) so that the rest of your code starts at the - if (!$action) { step, which starts over with whatever phrase happens to be the current one. Quote Link to comment Share on other sites More sharing options...
hamburgerlove413 Posted October 11, 2013 Author Share Posted October 11, 2013 (edited) Thank you! That fixed it. I hadn't thought of separating the NEXT PHRASE action from the main if statement. Edited October 11, 2013 by hamburgerlove413 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.