Brent
Brent

Reputation: 2485

PHP Function OOP Array Pull

Currently my file structure looks like this, everything works fine. But in my home.php, I would like to be able to pull out parts of the array, for example the users image.

$connect->LoadInformation->image 

How would i write this, or change my code for it to work? I really hope this make sense.

home.php

     $connect->LoadInformation($_COOKIE['steamID']);

class.php

        public function LoadInformation($steamID){

        $query = "SELECT * FROM user WHERE steamid = '$steamID'";
        $results = $this->con->get_results( $query );


        echo "<pre>";
        print_r($results);
        echo "<pre>";
    }

Current Output

    Array
(
    [0] => Array
        (
            [id] => 10
            [steamid] => 76561198026169223
            [profilestate] => 0
            [steam_user] => Dz - Lol u mad?
            [online] => 0
            [signup] => 2014-01-05 17:28:15
            [image] => http://media.steampowered.com/steamcommunity/public/images/avatars/b8/b85a568b25210a27a8413e4dc05c9f42a7cdaf22.jpg
            [description] => 
            [active] => 0
            [admin] => 0
        )

)

Upvotes: 0

Views: 675

Answers (2)

Andrey
Andrey

Reputation: 1496

Just convert it to object using (object) ...

public function LoadInformation($steamID){
   $query = "SELECT * FROM user WHERE steamid = '$steamID'";
   $results = $this->con->get_results( $query );
   return (object) array_shift($results);
}

And using it: $connect->LoadInformation(...)->image;

And i guessing the steamid will be unique in the table, in this case just pick the first returned result from the database query and then use it as you like.

Upvotes: 2

Change your class.php like this..

public function LoadInformation($steamID){
        $query = "SELECT * FROM user WHERE steamid = '$steamID'";
        $results = $this->con->get_results( $query );
        return $results;
    }

and your home.php

$arr =  $connect->LoadInformation($_COOKIE['steamID']);
echo $arr[0]['image']; //<-- prints  http://media.steampowered.com/steamcommunity/public/images/avatars/b8/b85a568b25210a27a8413e4dc05c9f42a7cdaf22.jpg

Upvotes: 1

Related Questions