Forum Moderators: coopster

Message Too Old, No Replies

using arrays from RETURN $arrayname?

         

Bigjohn

8:10 pm on Mar 6, 2004 (gmt 0)

10+ Year Member



Hi everyone... My work and troubleshooting continue....

I'm trying to work a series of functions like this:


function get_data($cnum,$invnum){
$query = 'SELECT * FROM sales WHERE invnum ='.$invnum.' AND relcid ='.$cnum;
$result = mysql_query($query);
$invoice = mysql_fetch_assoc($result);
$query = 'SELECT * FROM cdata WHERE cid ='.$cnum;
$result = mysql_query($query);
$customer = mysql_fetch_assoc($result);
$query = "SELECT * FROM ".$invoice['reltable']." WHERE item ='".$invoice['relitemno']."'";
$result = mysql_query($query);
$bought = mysql_fetch_assoc($result);
return $invoice;
return $customer;
return $bought;
}

and I want to use the RETURN arrays in another function like this:


function build_mail($invoice,$customer,$bought)

but it's not working! can someone please tell me why?

John

jonknee

9:10 pm on Mar 6, 2004 (gmt 0)

10+ Year Member



"Return" means "give back to whatever is storing the call to the function". You have nothing storing the return. Try something like:

$data = function build_mail($invoice,$customer,$bought)

Because of this, you normally only have one item returned, though it could be an array holding those three values.

Bigjohn

9:18 pm on Mar 6, 2004 (gmt 0)

10+ Year Member



there is a php page holding the function, and it grabs the variable for the first function out of the $_POST array:

$cnum=$_GET['cust'];
$invnum=$_GET['invoice'];

and the functions are called in order at the bottom of the same page:


get_data($cnum,$invnum);
build_mail($invoice,$customer,$bought);
customer_thanks($customer,$saddr,$bought,$invoice);
?>

perhaps my return statement should be:

return $invoice,$customer,$bought;

jonknee

9:38 pm on Mar 6, 2004 (gmt 0)

10+ Year Member



If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call.

Taken from the PHP Manual on Return [us4.php.net].

Look back at my first post. I think it's right.

Bigjohn

9:47 pm on Mar 6, 2004 (gmt 0)

10+ Year Member



Ok.

I'm just going to punch all the functions together then. I was trying to be modular, but I can't figuer out the right way to pass the values from one function to another... ARRGH.

John

jonknee

9:58 pm on Mar 6, 2004 (gmt 0)

10+ Year Member



You can call functions with functions inside... Like:

doThis(withThat($x), andThis($y), meToo($z));

Or have a master function that calls the others inside it. In other words, you can still be modular.

mykel79

5:03 pm on Mar 7, 2004 (gmt 0)

10+ Year Member



If you want to return a couple of values from one function, you can do it like this:

function blabla()
{
.
.
return array($val1,$val2,$val3);
}

list($a,$b,$c)=$blabla;

Hope that helps.

Netizen

9:57 am on Mar 8, 2004 (gmt 0)

10+ Year Member



There are a number of problems in the original implementation. When you return a value from the function it needs to be assigned e.g.

$data=get_data($cnum,$invnum);

There is also a problem in that you are trying to return three arrays from the function, which is not easy to do unless you construct something like:

function get_data($cnum,$invnum){
$query = 'SELECT * FROM sales WHERE invnum ='.$invnum.' AND relcid ='.$cnum;
$result = mysql_query($query);
$data['invoice'] = mysql_fetch_assoc($result);
$query = 'SELECT * FROM cdata WHERE cid ='.$cnum;
$result = mysql_query($query);
$data['customer'] = mysql_fetch_assoc($result);
$query = "SELECT * FROM ".$invoice['reltable']." WHERE item ='".$invoice['relitemno']."'";
$result = mysql_query($query);
$data['bought'] = mysql_fetch_assoc($result);
return $data;
}

and then you can extract the data using the appropriate key.

I hope that begins to help...