Jump to content

Incrementing a variable by 1


Go to solution Solved by mac_gyver,

Recommended Posts

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 by hamburgerlove413
Link to comment
https://forums.phpfreaks.com/topic/282771-incrementing-a-variable-by-1/
Share on other sites

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 by hamburgerlove413
  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.