473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 5050
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
5
by: vivekian | last post by:
Have to create a linked list of objects of the same type based on user input. The number of objects in the list is dynamic depending on the number input from the user. What would be the best way to...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
5
by: Sakcee | last post by:
python provides a great way of dynamically creating fuctions calls and class names from string a function/class name can be stored as string and called/initilzed e.g def foo(a,b): return...
8
by: mfc | last post by:
Suppose I have a Cookie class and a Factory Class. There are many types of Cookies and maybe one or more Factories with a ProcessCookie Method. Suppose all the PutInBox method does is decide what...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
26
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.