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
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
}

Link to comment
Share on other sites

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.