Dynamically Assign Associative Arrays to an Object

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Dynamically Assign Associative Arrays to an Object

Post by patrikG »

Hi all,

I want to automate things a bit and dynamically assign database-values to an associative array in a class-object.
------------------------
$db = $sql->arrays("SELECT * FROM $type WHERE id='32');
//query the DB with mysql_fetch_array() to retrieve values

foreach ($db as $key=>$value)
{
if (is_string($key))
{
$this->$type[$key]=$value;
// assigns the values to the corresponding class-object->property['array'] - ['array'] corresponds to the column-name in the database
}
}
------------------

'$type' in '$this->$type' can only be of four different values, each of which is defined in the class-constructor as array.

The problem is: it doesn't work.

Does anyone know a solution to this?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Shouldn't

Code: Select all

$this->$typeї$key]
be

Code: Select all

$this->typeї$key]
oh and you're missing a closing double-quote on your SQL statement.

Mac
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Actually, no:

$this->$type[$key] should be as it is.

The reason being that $type can only have 4 different values, corresponding to 4 pre-defined variables in the class-definitions. Thus $this->$type[...] should pick the correct type of these pre-defined variables and assign to it $value..

The missing double-quote in the SQL-text comes from...err...editing the previous post too much :p
The SQL-bit, the entire database-stuff is just fine and works, the problem is assigning values to this associative array

$type[$key]=$value;

Is this possible at all?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Sorry just scanned the code for obvious errors, will actually read it properly this time...

Mac
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try

Code: Select all

$this->{$type}ї$key]
Mac
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Sorry, I've been very scarce with posting some contextual code. Here goes:

Code: Select all

class forumClass
{
/////////////////////////////////////////////////////////
//	forum properties
/////////////////////////////////////////////////////////
	var $forum=array();
	var $thread=array();
	var $message=array();
	var $user=array();
	var $editor=array();
/////////////////////////////////////////////////////////
//	forum constructor
/////////////////////////////////////////////////////////
function forumClass()
	{
	if (!$connection) {include_once ("connect.inc");}
	}
/////////////////////////////////////////////////////////
//	forum methods
/////////////////////////////////////////////////////////

function readData($type)
	{
	// reads the values from the database (*note: seperate class with class_object >sql<*)
        $type    = "forum";  //set to "forum" in this example for simplicity
	$db 	  = $sql->arrays("SELECT * FROM forum WHERE id='".$this->forum&#1111;'id']."'");
	foreach ($db as $key=>$value)
	// assigns the values to the corresponding object->property&#1111;'array'] - &#1111;'array'] corresponds to the column-name in the database
		&#123;
		if (is_string($key))
			&#123;
			$this->$type=array($key=>$value);
			&#125;
		&#125;
	&#125;
&#125;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What does your function call for readData look like?

Mac
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

It's working

Post by patrikG »

:D

Thanks SO much, twigletmac!!! It's working!!!

The purpose of the whole exercise is basically to simplify things. Keep code and HTML-templates apart, but keep them bound together by the database.

Example: we have a file called "test.html"

Code: Select all

this is some HTML-Text <+>forum#name</+>
It's being read by my class partially posted above, which does the following:

- pull all relevant data from database-table "forum" and put it into an associative array.
- replace <+>forum#name</+> with the value of $this->forum["name"]

- rinse and repeat.

Thanks to your help it's working as intended!

Patrik
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

When all else fails use curly brackets :) ...

Glad it's working, these sort of problems can be incredibly frustrating...

Mac
Post Reply