Data base info as objects

C++, C#, Java, PHP, ect...
Post Reply
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Data base info as objects

Post 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 :)
New Site Coming Soon! Stay tuned :D
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Data base info as objects

Post 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
)
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Data base info as objects

Post 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
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: Data base info as objects

Post 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
New Site Coming Soon! Stay tuned :D
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Data base info as objects

Post 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);
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: Data base info as objects

Post 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 :(
New Site Coming Soon! Stay tuned :D
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Data base info as objects

Post 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);
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Coding”