Page 1 of 1

passing arrays

Posted: Fri Aug 16, 2002 11:33 pm
by markwenham
I have a function which prints an html form and populates fields with the values of an array that is passed into the function. The array is based on values in a db.

I want to use the same form to tell the user that there were submission errors after the page has posted to itself. I am thinking therefore that in this instance I want to call the function but use the values of the $_POST array to populate the fields. How can I pass the $_POST array as a whole to the function or alternatively refer to it as a default if no other array is passed e.g. something like

function myfunc($mydbarray = $_POST) where the = $_POST is the default if $mydbarray is not passed.

TIA, MW

Posted: Sat Aug 17, 2002 12:31 am
by llimllib
should work like the example you posted. Check out the manual on arguments, it explains the quriks of PHP arguments.

Posted: Sat Aug 17, 2002 12:51 am
by hob_goblin
last time i recalled, you can't accept arguments with default values as variables

you might want to try just

myfunc(){
$array = func_get_arg(0);
}


and call it like...

myfunc($_POST);

Posted: Sat Aug 17, 2002 7:49 am
by llimllib
from the man page I referenced:
A function may define C++-style default values for scalar arguments as follows:
You can include default values, but it's a little quirky. You'd probably be better off using hob's method and testing for the existence of variables.

I think I am getting there but...

Posted: Sat Aug 17, 2002 8:29 am
by markwenham
Ok I understand the method suggested by hob_goblin (thanks) but now have the following question --- what I am trying to pass is a mysql result array so I have queried the db, called mysql_fetch_assoc and assigned the results to $inrow. I then pass $inrow to my function which displays the output. I am only getting the first row of the array. If I try to use mysql_fetch_assoc again to loop thru the records it gives me an error

Code: Select all

mysql_fetch_array(): supplied argument is not a valid MySQL result resource
So how can I get at all the records in the array?
Thanks, MW

Posted: Sun Aug 18, 2002 11:08 am
by twigletmac
You need to pass the result set as using mysql_fetch_assoc() just returns one row from the set (as you discovered). If you did this:

Code: Select all

$result = mysql_query($sql);
then you should be passing $result to the function where you can then use mysql_fetch_assoc() to loop through the results.

Mac