Jump to content

Recommended Posts

How do I post a for loop array in the next page. I am printing Question ID and choices for these and would like the for loop to print Question ID and choice that are checked for these. If the choice is ticked then it shoul return a 1 if the choice is empty then it should print 0. and I would like to insert this to a new table.  How would I do this.

 

$intNum = 1;
$intnumber = 1;
while( $info = mysql_fetch_array( $sqll )){


echo "<input type='hidden' name=\"Que_ID\" value=\"{$info['Que_ID']}\" /> ";
  echo " $intNum, {$info['Que_Question']} <br />\n"; 
   $intNum++;
?>
<br>
<?PHP
for ($i =1; $i < 5; $i++) {  

echo "<input type=\"checkbox\" name=\"choice[{$intnumber}]\" />{$info['Que_Choice'.$i]}";  
  $intnumber++;

}

 

this is my form that print the question and it works fine.

 

<?PHP
foreach ($_POST["Que_ID"] as $question) { 
$_POST["choice"] = $choice;
echo "Question id: ".$question. $choice"<br />"; 

This is the post code but I know I need to do so much more but not sure how? Ayone help me or direct me to a source or tutorial or something.

Link to comment
https://forums.phpfreaks.com/topic/229651-_post-for-loop-array/
Share on other sites

change form to

<?php
while( $info = mysql_fetch_array( $sqll )){
    echo "<input type='hidden' name=\"Que_ID[]\" value=\"{$info['Que_ID']}\" /> ";
    echo " $intNum, {$info['Que_Question']} <br />\n<br />\n"; 
    for ($i =1; $i < 5; $i++) {
        echo "<input type=\"checkbox\" name=\"choice[{$info['Que_ID']}][]\" value=\"$i\" />{$info['Que_Choice'.$i]}";  
    }
}
?> 

and your action page to

<?PHP
foreach ($_POST["Que_ID"] as $question) { 
$_POST["choice"][$question] = $choice;
echo "Question id: ".$question. implode(', ',$choice")<br />";  

Thanks for the reply.

 

Your code will return

 

<input type="hidden" value="1" name="Que_ID[]">
<input type="checkbox" value="1" name="choice[1][]">
<input type="checkbox" value="2" name="choice[1][]">
<input type="checkbox" value="3" name="choice[1][]">
<input type="checkbox" value="4" name="choice[1][]">
<input type="hidden" value="2" name="Que_ID[]">

<input type="checkbox" value="1" name="choice[2][]">
<input type="checkbox" value="2" name="choice[2][]">
<input type="checkbox" value="3" name="choice[2][]">
<input type="checkbox" value="4" name="choice[2][]">

 

But I wanted my form to return

 

<input type="hidden" value="1" name="Que_ID[]">
<input type="checkbox" value="1" name="choice[1][]">
<input type="checkbox" value="2" name="choice[2][]">
<input type="checkbox" value="3" name="choice[3][]">
<input type="checkbox" value="4" name="choice[4][]">
<input type="hidden" value="2" name="Que_ID[]">

<input type="checkbox" value="1" name="choice[5][]">
<input type="checkbox" value="2" name="choice[6][]">
<input type="checkbox" value="3" name="choice[7][]">
<input type="checkbox" value="4" name="choice[8][]">

 

action page gives me an error

 

"Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\xampp\Testphp on line 6"

 

on line 6 is

 

echo "Question id: ".$question. implode(',',$choice")<br />";

 

the action code that I used for the choice is

 

foreach ($_POST["choice"] as $choice => $answer) { 
if(!empty($answer)){
	$val = 1;
	echo "Choice ID: .$choice. $val. is ticked <br/>\n";
				}
else{
	$val = 0;
	echo "Choice ID: $choice.$val";
	}}

 

outcome of this is

Choice ID: .1. 1. is ticked

Choice ID: .2. 1. is ticked

Choice ID: .3. 1. is ticked

Choice ID: .4. 1. is ticked

Choice ID: .5. 1. is ticked

Choice ID: .6. 1. is ticked

Choice ID: .7. 1. is ticked

Choice ID: .8. 1. is ticked

Choice ID: .9. 1. is ticked

Choice ID: .10. 1. is ticked

Choice ID: .11. 1. is ticked

Choice ID: .12. 1. is ticked

Choice ID: .13. 1. is ticked

Choice ID: .14. 1. is ticked

Choice ID: .15. 1. is ticked

Choice ID: .16. 1. is ticked

 

Now, I just want the question id to go with these. So que_ID(choice1, choice2,choice3, choice4) eg. 2(ticked,ticked,ticked,ticked). I can only display the result of ticked one and I cant display the empty one. If its not ticked then its a 0 and if its ticked it 1. so the outcome would be something like 2(1,1,0,1)

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.