You are here: Re: dynamic variable names for objects « PHP Programming Language « IT news, forums, messages
Re: dynamic variable names for objects

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация