views:

49

answers:

2

Hi. I have this object:

foreach(range(1,$oc->num_users) as $num) {
    $user = 'user' . $num;
    $$user = mysql_fetch_object(mysql_query("SELECT user".$num.", user".$num."_ready,  FROM oc_db WHERE leader='".$_SESSION['username']."' ")); 

This gives objects named user1..X

later I have a simular function like this, where I use the $$user->$user that represent a username to connect to the db to get additional information.

   $$user = mysql_fetch_object(mysql_query("SELECT x, y, z FROM user_db WHERE username='".$$user->$user."' "));

This also makes objects named user1..X, but instead of replacing the object created earlier I want to append this values to the object I created in the first function. $$user is the name of the objects. I would like to do something like $$user .= fetch_object in the last function, but off course it's not that simple.

A: 

In order to append one object to another you simply need to iterate through the first object and then assign each property you find to the second one:

$tmp = mysql_fetch_object(mysql_query("SELECT x, y, z FROM user_db WHERE username='".$$user->$user."' "));

foreach ($tmp as $key => $value) {
    $$user->$key = $value;
}
Saul
A: 

As a couple of asides, if you are trying to access the user property of the object, then you should use $$user->user, as $$user->$user will be trying to access a numbered property (e.g. for object $user12, you're trying to access $user12->user12). I would also consider using an array, as fredley suggested (i.e. use $user[$num] instead of $user = 'user'.$num).

To approach your original question, instead of using the .= concatenation assignment operator, you could try adding a method to your user class, such as append, then using it to append the fetched object to your current object like $user->append( fetch_object(...));. In the append method, you can define your own rules for how a fetched object is added to your current user object.

Aether