469,609 Members | 1,169 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

dynamic variable names for objects

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
Apr 3 '07 #1
5 4858
I need to use dynamic variable names but for objects.

Search for magic methods in the PHP help.

Best regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Apr 3 '07 #2
On Tue, 3 Apr 2007 15:40:20 +0200, "Rolf Mander" <ro*********@spam.comwrote:
>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]};
You'll need a temporary variable for the name.

$field = 'f_' . $fieldname[1];
echo $obj->$field;

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Apr 3 '07 #3
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.
Apr 3 '07 #4
Andy Hassall wrote:
You'll need a temporary variable for the name.

$field = 'f_' . $fieldname[1];
echo $obj->$field;
IMHO no need to put a temp var.
just do: echo $obj->{'f_'.$fieldname[1]};

Hendri
Apr 4 '07 #5
Thanks guys for helping me out !

Rolf
Apr 4 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by stewart.midwinter | last post: by
1 post views Thread by Nathan Bloomfield | last post: by
23 posts views Thread by Russ Chinoy | last post: by
5 posts views Thread by vivekian | last post: by
8 posts views Thread by mfc | last post: by
26 posts views Thread by Aaron \Castironpi\ Brady | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.