By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,968 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,968 IT Pros & Developers. It's quick & easy.

Accessing an array

P: 14
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]
Apr 25 '07 #1
Share this Question
Share on Google+
7 Replies


Motoma
Expert 2.5K+
P: 3,235
I am not entirely sure what you are doing here. Perhaps you could put together a simple test case that shows the problem you are having?
Apr 25 '07 #2

P: 14
It is displaying a number of questions sourced from the database (in the example, it selects 5 from the db and loops through and displays them).

The process_form function takes the user's answers to these questions and (when its working) will enter these into the db. These can then be checked against the correct answer and the user scored appropriately.

But in order to store them, i need the question number (not 1 - 5 from the form, but the number of the task as it is in the db). That is why i have the array in the loop to collect the "Q_Num". I then need this in the process_form function to use in the update query. (At the moment they are just being printed onto the screen to show it works).

The issue is accessing the array in the process_form function.

So it should go:
- user comes to page
- query run and question data returned in variable $result
- loop used to display question, opt_1...opt_4 for each row of $result
- in this loop, array $QuestionNum captures the db q_num for later use
- user completes test and clicks submit
- array $QuestionNums is passed to process_form
- this array and $_POST is used in db query to update table with answer ($_POST) in the relevant question row ($questionnums).

hope that makes sense!
Apr 25 '07 #3

code green
Expert 100+
P: 1,726
$QuestionNums is empty in process_form(). I am confused over which $QuestionNums you are hoping to use. You have it as a function argument and a global. But it is not declared as a global, only local in displayQuestions(), but from there you call process_form() but pass an empty array. I would re-design your functions a little better. Also I was taught that use of globals was a crime.
Apr 25 '07 #4

P: 14
I've tried so many different things that i had it declared in many places. I have changed it so it is now:
[PHP]
function displayQuestions($ModNum){
if (!isset($_POST['Submit'])){
show_form($ModNum);
}else{
process_form($QuestionNums);
}//end if
}//end function "displayQuestions"
[/PHP]

I want the array $QuestionNums to be created and filled in show_form and then passed to process_form to use the data placed in it - so the same array. Can i do that by simply putting return $QuestionNums at the end of the function show_form and then calling process_form as above?

I'm pretty new to PHP so all help is great. thanks
Apr 25 '07 #5

code green
Expert 100+
P: 1,726
You are trying to run before you can walk. Lets take it step by step.
I want the array $QuestionNums to be created and filled in show_form and then passed to process_form to use the data placed in it
Unfortunately, that is not what your code says:
[PHP]if (!isset($_POST['Submit'])){
show_form($ModNum);
}else{
process_form($QuestionNums);[/PHP]You call show_form() OR call process_form()
I think this is what you mean
[PHP]if (!isset($_POST['Submit'])){
$QuestionNums = show_form($ModNum);
process_form($QuestionNums);
}[/PHP]
Can i do that by simply putting return $QuestionNums at the end of the function show_form
Yes.

[PHP]function show_form($ModNum)
{

$QuestionNums = array();
//fill the array etc
return $QuestionNums;
}[/PHP]
This will take you a little closer
Apr 25 '07 #6

P: 14
OK thats cool. But the bit at the start does both of them regardless:
[PHP]
if (!isset($_POST['Submit'])){
$QuestionNums = show_form($ModNum);
process_form($QuestionNums);
}
[/PHP]
I need it to display the questions (the show_form function) and when submit is pressed it processes the form (process_form function).

With the previous suggestion, it does both function and then when submit is pressed it just presents a page with the standard page template, but nothing in the content part, as it sees the array $QuestionNums past to it as empty (and hence has nothing to print to the screen).
Apr 25 '07 #7

code green
Expert 100+
P: 1,726
I need it to display the questions (the show_form function) and when submit is pressed it processes the form (process_form function).
As I previously said, you need to look at your design. Why do you want to pass the questions to the process() function? Surely you want to pass the answers.
But the bit at the start does both of them regardless:
Regardless of what? If you want the questions displayed upon initial entry to the script then the answers processed upon form submission this is quite easy to achieve, but I am still not sure what you are trying to do .
Apr 26 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.