Hello.
First of all I need to excuse myself, I am total noob in OOP so I'm a little bit confused with classes and all that stuff....
What I need is to run some query on database and populate 'an object' (class, right?) with the values I got from my query.
This is my database communication class in default_table.php file (constants.php is just host/user/pass stuff...)
<?php
require_once 'constants.php';
class default_table {
public $tablename; // table name
public $dbname; // database name
public $fieldlist; // list of fields in this table
public $data_array = array(); // data from the database
function __construct(){
$this->tablename = 'default';
$this->dbname = 'default';
$this->fieldlist = array('column1', 'column2', 'column3');
$this->fieldlist['column1'] = array('pkey' => 'y');
} // constructor
function getData ($where){
global $dbconnect, $query;
$dbconnect = db_connect($this->dbname);
if (empty($where)) {
$where_str = NULL;
} else {
$where_str = "WHERE $where";
}
$query = "SELECT * FROM $this->tablename $where_str";
$result = mysql_query($query, $dbconnect);
while ($row = mysql_fetch_assoc($result)) {
$this->data_array = $row;
}
mysql_free_result($result);
return $this->data_array;
}
}
When I need some information from my db I do it this way:
<?php
require_once 'default_table.php';
require_once 'userVO.php';
class user extends default_table {
function __construct(){
$this->tablename = 'users';
$this->dbname = 'database_name';
$this->fieldlist = array('ID', 'UID', 'username', 'password', 'userlevel');
$this->fieldlist['ID'] = array('pkey' => 'y');
}
function getUser($user, $pass){
$user_cond = "username='".$user."' AND password = '".md5($pass)."'";
$user = $this->getData($user_cond);
print_r($user);
}
}
At this point, when I use getUser("user", "pass"), I get correct values from a table in my array. print_r gives me something like this:
[FONT="Times New Roman"]Array ( [ID] => 1 [UID] => 2 [username] => user_from_table [password] => encrypted_password [userlevel] => 1 etc....)[/FONT]
What I would like to do here is to pass values from this array to another object (userVO.php which I included in last code). This 'value object' is just a simple 'holder' for retrieved data and it looks like this:
<?php
class userVO {
public $ID;
public $UID;
public $username;
public $password;
public $userlevel;
}
How can I do that?
Thanks for help!