Jump to content

FOREACH HELP NEEDED


phpretard

Recommended Posts

The reason I need the foreach is because there are 45 catID[] that should correspond with 45 score[]

 

if catID's value is 5 and score is checked the vars I need would be: "$catID = 5 , $score = 1"

 


<?

foreach($_POST['catID'] as $score){

//insert values "$catID = 5 , $score = 1"

}

?>

<form>

// in a while loop

<input type="text" name="catID[]" value="<? echo $test['id']; ?>" readonly />
<input type="checkbox" name="score[]" value=\"1\" /> incorrect

// end in a while loop

<br />
<input type="submit" name="submit_test" value="Submit" />
</form>

 

Thank you for the help!

Link to comment
Share on other sites

This might help explain...

 


foreach($_POST['catID'] as $catID){



if ($_POST['score'] == "1"){ // represents a checkbox with a value of 1


$insert = mysql_unbuffered_query(" insert into test_results values('".$_POST['id']."', '$catID', '1') ");	


}else{


$insert = mysql_unbuffered_query(" insert into test_results values('".$_POST['id']."', '$catID', '0') ");		

}

}

Link to comment
Share on other sites

There are 45 text fields (catID) and 45 corresponding checkboxes (score)

 

<form>

 

// in a while loop that stops at 45

 

<input type="text" name="catID[]" value="<? echo $test['id']; ?>" readonly />

<input type="checkbox" name="score[]" value=\"1\" /> incorrect

 

// end in a while loop

 

<br />

 

<input type="submit" name="submit_test" value="Submit" />

 

</form>

Link to comment
Share on other sites

Ah. Assuming the keys match up, you should be able to use:

 

foreach($_POST['catID'] as $key => $catID){
    if ($_POST['score'][$key] == "1"){ // represents a checkbox with a value of 1
        $insert = mysql_unbuffered_query(" insert into test_results values('".$_POST['id']."', '$catID', '1') ");   
    }else{
        $insert = mysql_unbuffered_query(" insert into test_results values('".$_POST['id']."', '$catID', '0') ");      
    }
}

 

You'll want to secure your inputs though!

 

Link to comment
Share on other sites

I am posting...

 

I just realized the checkboxes ("1") are not being inserted with the right catID.

 

I checked numbers 1, 3, 5, 7

 


Here is the actual output from the code:

1 = 1
2 = 1
3 = 1
4 = 1
5 = 0
6 = 0
7 = 0
8 = 0
9 = 0
10 = 0
up to 45

It should be:

1 = 1
2 = 0
3 = 1
4 = 0
5 = 1
6 = 0
7 = 1
8 = 0
9 = 0
10 = 0
up to 45

 

any thoughts?

Link to comment
Share on other sites

A picture might help?

 

row "math_cat" is the number in the form.

 

WRONG "checkbox" has a value of 1

 

	
foreach($_POST['catID'] as $key => $catID){    

	if ($_POST['score'][$key] == "1"){ 
		$insert = mysql_unbuffered_query(" insert into test_results values('".$_POST['id']."', '$catID', '1', '', '', '') ");       

	}else{   
		$insert = mysql_unbuffered_query(" insert into test_results values('".$_POST['id']."', '$catID', '0', '', '', '') "); 

	}
}
}

 

[attachment deleted by admin]

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.