Page 1 of 1

Data base info as objects

Posted: Sun Aug 08, 2010 3:43 pm
by Torniquet
Ok recently been looking into the use of objects, arrays and classes after playing about with the phpbb backend.

currently i have figured out how to rack info up into an array (not hard i know lol but i never used to and it makes things alot neater) and then turn them into objects to use

so now i can access basic info such by doing the following

Code: Select all

$userResults = $userInfo->query("SELECT username, user_colour, user_lang, user_gender FROM phpbb_users WHERE user_id='$uid'");

$userRow = $userResults->fetch_object();

$playerInfo = (object)array(
			'USERNAME'		=>	($userRow->user_colour != "")? "<font color='#$userRow->user_colour'>$userRow->username</font>" : $userRow->username,
			'USER_LANG'		=>	$userRow->user_lang,
			'USER_GENDER'		=>	$userRow->user_gender,
);
then displaying them as objects like so

Code: Select all

<?php
echo $lang->GREETING_1 . $playerInfo->USERNAME . $lang->GREETING_2;
?>
now what i want to know, is how to do the following and if its possable.

I have a configuration table which would appear like so..

Config_option || Config_Value

option 1 || value1
option 2 || value2

etc...

what i want to achive is to load all the comonly used config options into an array, but write the keys (if thats what they are called lol) as the name of the option name. like so and access them as shown

Set them in an array like this...

Code: Select all

$config = (object)array(
$option1 => $value1,
$option2 => $value 2
);
then access them like

Code: Select all

$config->option1;
$config->option2;
you get the picture.

can anyone explain how and if this is possable to do. thanks :)

Re: Data base info as objects

Posted: Tue Aug 10, 2010 9:21 am
by Chris
The easiest way would be to write your array in PHP and then encode it into JSON(JavaScript Object Notation), and save it. This is meant for use with JavaScript to communicate with the server but it's handy for other stuff.

Code: Select all

$array = array( 'username' => "LOL", 'user_lang' => 'EN', 'user_gender' => 'male' );
echo json_encode($array);
 
this should be the output:

Code: Select all

{
    "username" : "LOL",
    "user_lang" : "EN",
    "user_gender" : "male"
}
Just like writing a normal object in JavaScript.

The handy thing about this is you can easily make multidimensional arrays

Code: Select all

$array = array('lol' => array('lol', array('haha', 'wahaha', array('waahahahahaaha')), 'ha'));
echo json_encode($array);
 

Code: Select all

{
    "lol" : [
                 "lol",
                 [
                     "haha",
                     "wahaha",
                     ["waahahahahaaha"]
                 ],
                 "ha"
             ]
}
to decode it with PHP use json_decode()

Code: Select all

print_r(json_decode('{"username" : "LOL","user_lang" : "EN", "user_gender" : "male"}')); 
 
will put out

Code: Select all

stdClass Object
(
    [username] => LOL
    [user_lang] => EN
    [user_gender] => male
)

Re: Data base info as objects

Posted: Tue Aug 10, 2010 3:07 pm
by Chris
Sorry, I just read through your post again and I didn't fully understand it the first time.

Just ignore that previous post,

What you could do is make the classes constructor set the variables when it's called.

Code: Select all

class config {

    function __construct()
    {
        $array = array( 'option1' => "value1",
                        'option2' => "value2");
        foreach( $array as $key => $value )
        {
            $this->$key = $value;
        }
    }
    
}

$config = new config;
echo $config->option1; // value1
 

Re: Data base info as objects

Posted: Thu Aug 12, 2010 6:31 am
by Torniquet
thanks chris :)

is there no way i can make the array auto construct? or have the foreach build straight from the database?

like..

Code: Select all

$config = $gameInfo->mysqli_fetch_object();

foreach($config as $config_option => config_$value){
  $this->$option = $value
}
rather than having to manually build the array? which was what i was wanting to avoid

Re: Data base info as objects

Posted: Thu Aug 12, 2010 8:59 am
by Chris
You don't have to build the array manually. I just wrote that array in there as an example.

Code: Select all

class arrayToObject
{
    function __construct($array)
    {
        foreach( $array as $key => $value )
        {
            $this->$key = $value;
        }
    }
}

$config = new arrayToObject($gameInfo->mysqli_fetch_object());
print_r($config);
 
Here's another alternative

Code: Select all

function arrayToObject($array)
{
    $object = new stdClass;
    foreach( $array as $key => $value )
    {
        $object->$key = $value;
    }
    return $object;
}

$config = arrayToObject($gameInfo->mysqli_fetch_object());
print_r($config);
 

Re: Data base info as objects

Posted: Sat Nov 06, 2010 5:09 pm
by Torniquet
time for a bump...

I still cant get this working how i want it to ¬¬

this is my code

Code: Select all

require ("connect.php");

function arrayToObject($array)
{
    $object = new stdClass;
    foreach( $array as $key => $value )
    {
        $object->$key = $value;
    }
    return $object;
}
$sql = $gameInfo->query("SELECT * FROM info_config");
$config = arrayToObject($sql->fetch_object());
print_r($config);
as stated by chris...

but this is the output :(

Code: Select all

stdClass Object ( [config_option] => an_option [config_value] => a_value ) 
i want it to be as the following

Code: Select all

stdClass Object ( [an_option] =>  a_value ) 
i have tried alsorts but still comming up short of my goal lol.

anyone know how to sort this :(

Re: Data base info as objects

Posted: Sat Nov 06, 2010 8:30 pm
by Chris

Code: Select all

$array = array();

$query = $query("SELECT * FROM `info_config`");
while( $row = mysql_fetch_assoc($query) )
{
    $array[$row['config_option']] = $row['config_value'];
}

$config = arrayToObject($array);
print_r($config);