Vidya_tr Posted April 25, 2009 Share Posted April 25, 2009 I am developing a page for displaying a quiz. On the page for creating the questions i have the text fields for entering the choices for a question.the text fields are named as array 'choice[]'.Beside each textfield there is a list box for setting the grade for the choice.The list boxes are named as array 'select[]'. On submitting the page ,the choices and their corresponding grade are inserted to database .The values are taken using $_POST from the submitting page. The code for inserting is written as follows using while loop and list. while (list(,$option) = each($_POST['choice']) && list(,$grade) = each($_POST['select'])) { mysql_query("insert into assign_answers(assignID,question_number,answer,grade) values('$assignid','$qn_num','$option','$grade')"); } But the answer field ,that is the $_POST['choice'] value is not getting inserted.other fields are inserted. I couldnt find what the problem is.Please help me to find where I went wrong in using the while loop and list.... Quote Link to comment https://forums.phpfreaks.com/topic/155591-solved-problem-in-while-loop-and-list/ Share on other sites More sharing options...
mikesta707 Posted April 25, 2009 Share Posted April 25, 2009 im not entirely sure, but try changing list(,$option) to list($option) Quote Link to comment https://forums.phpfreaks.com/topic/155591-solved-problem-in-while-loop-and-list/#findComment-818871 Share on other sites More sharing options...
Vidya_tr Posted April 25, 2009 Author Share Posted April 25, 2009 No..Its not working properly.. Now the answer is not inserted and grade value inserted is not correct value chosen in the list box Quote Link to comment https://forums.phpfreaks.com/topic/155591-solved-problem-in-while-loop-and-list/#findComment-818873 Share on other sites More sharing options...
mikesta707 Posted April 25, 2009 Share Posted April 25, 2009 oh my apologies, I wasnt familiar with what you were doing there, but now I understand. hmm that seems like it should work, but perhaps without a variable to assign the value of the key that each passes, it might not function correctly. Try making, like, a garbage variable or something that you use as the first parameter of the list function, IE instead of what you have, write while (list($garbage, $option... etc. But I could be totally wrong like last time. Hope that helps, sorry about first post Quote Link to comment https://forums.phpfreaks.com/topic/155591-solved-problem-in-while-loop-and-list/#findComment-818879 Share on other sites More sharing options...
Vidya_tr Posted April 25, 2009 Author Share Posted April 25, 2009 using the loop like this... while (list($garbage1,$option) = each($_POST['choice']) && list($garbage2,$grade) = each($_POST['select'])) { mysql_query("insert into assign_answers(assignID,question_number,answer,grade) values('$assignid','$qn_num','$option','$grade')"); } nothing gets inserted in to the database.... Quote Link to comment https://forums.phpfreaks.com/topic/155591-solved-problem-in-while-loop-and-list/#findComment-818890 Share on other sites More sharing options...
Daniel0 Posted April 25, 2009 Share Posted April 25, 2009 for ($i = 0, $max = sizeof($_POST['choice']); $i < $max; $i++) { // $_POST['choice'][$i] // $_POST['select'][$i] } Assumes that the size of the two arrays are equal though. Quote Link to comment https://forums.phpfreaks.com/topic/155591-solved-problem-in-while-loop-and-list/#findComment-818899 Share on other sites More sharing options...
Vidya_tr Posted April 25, 2009 Author Share Posted April 25, 2009 while(list($garbage1,$option)=each($_POST['choice'])) echo $option; while (list($garbage1,$option) = each($_POST['choice']) && list($garbage2,$grade) = each($_POST['select'])) { echo "OPTION:".$option."<br>";echo $grade; mysql_query("insert into assign_answers(assignID,question_number,answer,grade) values('$assignid','$qn_num','$option','$grade')"); } when I print the $option outside the while loop its shows the value of option. But at the same time inside the while with list() the value doesnt print.(both 'option' and 'grade' doesnt print) After commenting the the while loop for printing the values ,the grade gets printed in the while loop with list(),but OPTION doesnt print. the problem might be in using the while with list()... Can I use two list() conditions in a while? Please help me to solve this... Quote Link to comment https://forums.phpfreaks.com/topic/155591-solved-problem-in-while-loop-and-list/#findComment-818902 Share on other sites More sharing options...
Vidya_tr Posted April 25, 2009 Author Share Posted April 25, 2009 for ($i = 0, $max = sizeof($_POST['choice']); $i < $max; $i++) { $option=$_POST['choice'][$i]; echo $option; $grade=$_POST['select'][$i]; echo $grade; mysql_query("Insert into assign_answers(assignID,question_number,answer,grade) values('$assignid','$qn_num','$option','$grade')"); } It prints the values in the for loop.. but doesnt get inserted ..... Quote Link to comment https://forums.phpfreaks.com/topic/155591-solved-problem-in-while-loop-and-list/#findComment-818907 Share on other sites More sharing options...
Vidya_tr Posted April 25, 2009 Author Share Posted April 25, 2009 Yes.. finally I got it with this... for ($i = 0, $max = sizeof($_POST['choice']); $i < $max; $i++) { $option=$_POST['choice'][$i]; $grade=$_POST['select'][$i]; mysql_query("insert into assign_answers(assignID,question_number,answer,grade) values('$assignid','$qn_num','$option','$grade')"); } thank you for helping me... Quote Link to comment https://forums.phpfreaks.com/topic/155591-solved-problem-in-while-loop-and-list/#findComment-818912 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.