passing arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
markwenham
Forum Newbie
Posts: 13
Joined: Fri Aug 16, 2002 11:33 pm

passing arrays

Post 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
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

should work like the example you posted. Check out the manual on arguments, it explains the quriks of PHP arguments.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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);
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
markwenham
Forum Newbie
Posts: 13
Joined: Fri Aug 16, 2002 11:33 pm

I think I am getting there but...

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply