473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Accessing an array

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
7 1616
Motoma
3,237 Expert 2GB
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
Franky
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
1,726 Expert 1GB
$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
Franky
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
1,726 Expert 1GB
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
Franky
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
1,726 Expert 1GB
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

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

Similar topics

1
by: Gregor | last post by:
I'm having problems accessing a global array variable from within a function. I have something like this: ------------------------------------ var detail = new Array( 20 ); detail = "hello"; ...
27
by: Daniel Lidström | last post by:
Hello! I want to work with individual bytes of integers. I know that ints are 32-bit and will always be. Sometimes I want to work with the entire 32-bits, and other times I want to modify just...
3
by: Christopher Benson-Manica | last post by:
I appreciate all the responses to my earlier post about accessing named elements. However, I'm still wondering about my actual problem, which is that I need to initialize some arrays of named...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
3
by: mtjarrett | last post by:
i having trouble accessing the values from superglobal arrays. there are two situations but i'm pretty sure it's the same problem. here's the deal: on page1.php i have several check boxes. ...
7
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a...
4
by: vanderr | last post by:
Hi everyone. I've recently been asigned a program where we are required to create a 2D array dynamically and then do stuff with it. We are not allowed to "cheat" and use the 1D method. in the...
5
by: Paul Brettschneider | last post by:
Hello, I have a global static array of structs and want to access a given element using an identifier. I don't want to use the element subscript, because it will change if I insert elements...
2
by: ...vagrahb | last post by:
I am having accessing individual rows from a multidimensional array pass to a function as reference CODE: function Declaration int Part_Buffer(char (*buffer),int Low, int High)
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.