Following discussion in a previous post, i have used an array inside a loop to gather results:
[PHP]
//Function called from another page
function displayQuestions($ModNum){ //modnum is used in the db query
global $QuestionNums;
if (!isset($_POST['Submit'])){
show_form($ModNum);
}else{
process_form($QuestionNums);
}//end if
}//end function "displayQuestions"
function show_form($ModNum)
{
global $result, $QuestionNums;
dbConnect();
getQuestion($ModNum); //call to query in other file. result is $result?>
<p>Introduction text here</p>
<TABLE>
<FORM method="post" name="Answers" action="<?php echo $PHP_SELF; ?>">
<?php
$QuestionNums = array();
$a=1;
while ($row = mysql_fetch_assoc($result))
{ ?>
<TABLE>
<TR><TD><b><? echo "$a. ", $row ['Question']. "<br/>" ?></b></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="1"> <? print $row ['Opt_1']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="2"> <? print $row ['Opt_2']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="3"> <? print $row ['Opt_3']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="4"> <? print $row ['Opt_4']. "<br/>" ?></TD></TR>
</TABLE>
<?php
$i=$a;
$num.$i = $row['Q_Num'];
$QuestionNums[]=$num.$i;
print "<br/>";
$a++;
}//end while
//This is just printing them on screen to show they are there - it works
foreach ($QuestionNums as $value) {
echo $value."<br>";
}
?>
<TR><TD><input type="submit" name="Submit" value="Submit"></TD></TR>
</FORM>
</TABLE>
<?php
}//end function "show_form"
function process_form($QuestionNums)
{
global $result, $QuestionNums;
//Call to ModDBQueries to connect to the database
dbConnect();
//TEST TO SEE IF IT WORKS
foreach ($QuestionNums as $value) {
echo $value."<br>";
}
}//end function "process_form"
[/PHP]
I want to add the values to the array in the first function, then use it in the second (at the moment i'm justing displaying them to make sure it works).
I can't seem to pass it between them - would this have something to do with the fact that i have 1 function that says if submit, do this function, else do that function (and it cant pass the array/isnt returning it to this 1st function)?
With the code above, i get the array printing fine just above the submit button, but when submit is pressed i get:
Warning: Invalid argument supplied for foreach() in C:\Program Files\xampp\htdocs\Lilly\PageFiles\ModDisplayQuest ions2.php on line 78
Line 78 is "foreach ($QuestionNums as $value) {" in the process_form function.
I have tried it using the following layout as well with no luck:
[PHP]
if (!isset($_POST['Submit'])){
//all the stuff from show_form function here
} else {
//all the stuff from process_form here
[/PHP]