Page 1 of 1

Dynamically Assign Associative Arrays to an Object

Posted: Thu Aug 15, 2002 5:53 am
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?

Posted: Thu Aug 15, 2002 6:03 am
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

Posted: Thu Aug 15, 2002 6:18 am
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?

Posted: Thu Aug 15, 2002 6:26 am
by twigletmac
Sorry just scanned the code for obvious errors, will actually read it properly this time...

Mac

Posted: Thu Aug 15, 2002 6:34 am
by twigletmac
Try

Code: Select all

$this->{$type}ї$key]
Mac

Posted: Thu Aug 15, 2002 6:45 am
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;

Posted: Thu Aug 15, 2002 7:01 am
by twigletmac
What does your function call for readData look like?

Mac

It's working

Posted: Thu Aug 15, 2002 7:21 am
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

Posted: Thu Aug 15, 2002 7:44 am
by twigletmac
When all else fails use curly brackets :) ...

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

Mac