phpretard Posted February 8, 2010 Share Posted February 8, 2010 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! Quote Link to comment Share on other sites More sharing options...
Adam Posted February 8, 2010 Share Posted February 8, 2010 Not sure I follow you. You want to loop throug the catIDs array, and if it equals 5, and if the corresponding index in the score array is checked (1/true), you want to...? Quote Link to comment Share on other sites More sharing options...
phpretard Posted February 8, 2010 Author Share Posted February 8, 2010 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') "); } } Quote Link to comment Share on other sites More sharing options...
Adam Posted February 8, 2010 Share Posted February 8, 2010 So there's just 1 "score" checkbox? What's the current problem / errors you're receiving with that code? Quote Link to comment Share on other sites More sharing options...
phpretard Posted February 8, 2010 Author Share Posted February 8, 2010 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> Quote Link to comment Share on other sites More sharing options...
Adam Posted February 8, 2010 Share Posted February 8, 2010 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! Quote Link to comment Share on other sites More sharing options...
phpretard Posted February 8, 2010 Author Share Posted February 8, 2010 That is as close as I have gotten thus far. So I ticked the first 5 checkboxes and it only inserted a single "1"; the other 4 were "ignored". Any thoughts? What do you mean "secure my inputs"? Quote Link to comment Share on other sites More sharing options...
phpretard Posted February 8, 2010 Author Share Posted February 8, 2010 FREAKS RULE! THANK YOU!!! Quote Link to comment Share on other sites More sharing options...
Adam Posted February 8, 2010 Share Posted February 8, 2010 Not sure if you realised this but the default form action is "get" and you're using $_POST .. Edit: what was the problem? Quote Link to comment Share on other sites More sharing options...
phpretard Posted February 8, 2010 Author Share Posted February 8, 2010 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? Quote Link to comment Share on other sites More sharing options...
phpretard Posted February 8, 2010 Author Share Posted February 8, 2010 I don't think the keys match up... Quote Link to comment Share on other sites More sharing options...
phpretard Posted February 8, 2010 Author Share Posted February 8, 2010 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] 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.