Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/155591-solved-problem-in-while-loop-and-list/
Share on other sites

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

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

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... :(

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

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... :)

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.