|
Posted by Schraalhans Keukenmeester on 04/03/07 23:27
Rolf Mander wrote:
> Hi,
> I need to use dynamic variable names but for objects.
> As you know something like that works fine:
>
> $variable="content";
> $part="able";
> echo ${"vari".$part};
> // gives out content
>
> but i need to use a database object after a mysql query something like that
>
> $sqlresult= mysql_query("SELECT * FROM t_table_xy");
> while (($obj = @mysql_fetch_object($sqlresult)) {
> echo ${"obj->f_".$fieldname[1]};
> }
>
> but this dont work, I suppose because of the -> pointer.
> has someone an idea how to solve that ?
>
> thanks
> rolf
>
>
Instead of the direct echo way, first store the total string in a variable.
<?PHP
// your code up to and incl. the while loop
$output= "$obj->f_{$fieldname[1]}";
echo $output;
?>
Using the magic __call() method may be the more formally correct, if
there is such a thing...
HTH
Sh.
Navigation:
[Reply to this message]
|