Page 1 of 1

Problem passing variables to functions.

Posted: Wed Nov 11, 2009 5:13 am
by tpotman777
Hello.

Probably pretty easy stuff, but can't figure it out:

I have a string list of variables, for instance:
$vars = '$a1,$a2,$a3';

which I would like to pass to list() -function:
list($vars) = $db->sql_fetchrow($result)

Of cours now $vars gets the value from the query, but what I would like list to have is:
list($a1,$a2,$a3) = $db->sql_fetchrow($result)

Any idea how I could output the contents of $vars to the list() -function?

Thanks,
Tom

Re: Problem passing variables to functions.

Posted: Wed Nov 11, 2009 5:47 am
by jackpf
I think the only way to do this is with eval().

Re: Problem passing variables to functions.

Posted: Wed Nov 11, 2009 6:42 am
by tpotman777
jackpf wrote:I think the only way to do this is with eval().
Umm, how could I implement that in my code?

Re: Problem passing variables to functions.

Posted: Wed Nov 11, 2009 6:58 am
by jackpf
Something like:

Code: Select all

$vars = '$a1,$a2,$a3';
 
eval('list('.$vars.') = $db->sql_fetchrow($result);');
I think.

Re: Problem passing variables to functions.

Posted: Wed Nov 11, 2009 7:17 am
by tpotman777
Ok, looks reasonable! I actually also have 'while' -statement in there, but just can't get the syntax right:

eval('while list('.$vars.') = $db->sql_fetchrow($result){ echo "something"; }');

gives: Parse error: syntax error, unexpected T_LIST, expecting '('

Any clues on that?

Thanks!
Tom

jackpf wrote:Something like:

Code: Select all

$vars = '$a1,$a2,$a3';
 
eval('list('.$vars.') = $db->sql_fetchrow($result);');
I think.

Re: Problem passing variables to functions.

Posted: Wed Nov 11, 2009 7:21 am
by tpotman777
Ok, got it working, thanks for your help!

-T
tpotman777 wrote:Ok, looks reasonable! I actually also have 'while' -statement in there, but just can't get the syntax right:

eval('while list('.$vars.') = $db->sql_fetchrow($result){ echo "something"; }');

gives: Parse error: syntax error, unexpected T_LIST, expecting '('

Any clues on that?

Thanks!
Tom

jackpf wrote:Something like:

Code: Select all

$vars = '$a1,$a2,$a3';
 
eval('list('.$vars.') = $db->sql_fetchrow($result);');
I think.

Re: Problem passing variables to functions.

Posted: Wed Nov 11, 2009 7:24 am
by jackpf
Cool.