Jump to content

[SOLVED] Syntax in for loop?


galvin

Recommended Posts

Just curious if anything jumps out at anyone as being wrong syntax wise with this code below.  I ask because for example, if $_SESSION['answer1'] is NOT set, it is NOT getting set to "wrong" with this code.  And therefore, when it redirects to the desired page, $SESSION[answer1'] is still not set to anything.

 

However, for testing purposes, if I actually change the line...

$_SESSION['answer$i'] = "wrong";

...to be...

$_SESSION['answer1'] = "wrong";

...then it IS set to "wrong" after the redirect.

 

In other words, if $i ever equaled "1", it would work. So there must be something wrong with the way the for loop is using my $i variable because it's obviously not getting to the point where it sees $i as the value "1".

 

Any ideas?...

 

if (isset($_POST['submit']) && ($_POST['submit'] == "Stop")) {
for ($i=0; $i < 2; $i++) {

	if (isset($_SESSION['answer$i'])) {
	//do nothing
	} else {
	$_SESSION['answer$i'] = "wrong";	
	}
}

redirect_to($_SESSION['currentpage'] . "?quizid=" . $_SESSION['quizid']);

} else { 
// do nothing
}

Link to comment
https://forums.phpfreaks.com/topic/158686-solved-syntax-in-for-loop/
Share on other sites

Try this instead:

 

if (isset($_POST['submit']) && ($_POST['submit'] == "Stop")) {
for ($i=0; $i < 2; $i++) {

	if (isset($_SESSION['answer'.$i])) {
	//do nothing
	} else {
	$_SESSION['answer'.$i] = "wrong";	
	}
}

redirect_to($_SESSION['currentpage'] . "?quizid=" . $_SESSION['quizid']);

} else { 
// do nothing
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.