Hello all! I'm pretty much clueless as to how to write code. I saw the following script from http://thinkdiff.net/facebook/users-demographic-data-from-facebook/ so I decided to play around with it but I just can't get it to work. I created the table shown and also a file bd.php where it has the info for database connection, I'm pretty much stuck. what do I write to insert that information into the database? I have no idea what the '$this' part is supposed to do. Thanks for any input!

<?php
    include_once "fbmain.php";
    $config['baseurl']  =   "http://thinkdiff.net/demo/newfbconnect1/demographicdata/index.php";
 
    //if user is logged in and session is valid.
    if ($fbme){
        //collect some data using legacy api
        $param  =   array(
            'method'     => 'users.getinfo',
            'uids'       => $fbme['id'],
            'fields'     => 'birthday_date, interests, locale, political, relationship_status, affiliations',
            'callback'   => ''
        );
 
        try{
            $info           =   $facebook->api($param);
        }
        catch(Exception $o){
            error_log("Legacy Api Calling Error!");
        }
        //using graph api
        //array data
        $workInfo       =   getWorkInfoAsString($fbme);
        $education      =   getEducationAsString($fbme);
 
        $moviesArr      =   $facebook->api("/me/movies");
        $musicArr       =   $facebook->api("/me/music");
        $televisionArr  =   $facebook->api("/me/television");
 
        //format some api data
        $movies         =   getArrayDataAsString($moviesArr['data']);
        $music          =   getArrayDataAsString($musicArr['data']);
        $television     =   getArrayDataAsString($televisionArr['data']);
 
        //data from legacy api
        $networks       =   '';
        if (!empty($info[0]['affiliations'])){
            $flag       =   true;
            foreach ($info[0]['affiliations'] as $item){
                if (!$flag)  $networks.= ' # ';
                $networks   .=  $item['name'];
                $flag   =   false;
            }
        }
 
        $now            =   date("Y-m-d G:i:s");
        $insData        =   array(
            'uid'                   =>  $fbme['id'],
            'first_name'            =>  $fbme['first_name'],
            'last_name'             =>  $fbme['last_name'],
            'email'                 =>  isset($fbme['email']) ? $fbme['email'] : '',
            'link'                  =>  $fbme['link'],
            'affiliations'          =>  $networks,
            'birthday'              =>  $info[0]['birthday_date'],
            'current_location'      =>  isset($fbme['location']['name']) ? $fbme['location']['name'] : '',
            'education_history'     =>  $education,
            'work'                  =>  $workInfo,
            'hometown_location'     =>  isset($fbme['hometown']['name']) ? $fbme['hometown']['name'] : '',
            'interests'             =>  $info[0]['interests'],
            'locale'                =>  $info[0]['locale'],
            'movies'                =>  $movies,
            'music'                 =>  $music,
            'political'             =>  $info[0]['political'],
            'relationship_status'   =>  $info[0]['relationship_status'],
            'sex'                   =>  isset($fbme['gender']) ? $fbme['gender'] : '',
            'tv'                    =>  $television,
            'status'                =>  '0',
            'created'               =>  $now,
            'updated'               =>  $now,
        );
 
        //$this->db->insert('demographic', $insData);
    }
 
    function getWorkInfoAsString($fbme, $delim = '#', $partDelim = ' | '){
        $info       =   "";
        $flag           =   false;
        if (empty($fbme['work'])) return '';
 
        foreach($fbme['work'] as $item){
            if ($flag)
                $info .= $partDelim;
            $flag   =   true;
            $info   .=  (isset($item['employer']['name']) ? $item['employer']['name'] : '' ). $delim .
                        (isset($item['location']['name']) ? $item['location']['name'] : '' ). $delim .
                        (isset($item['position'])         ? $item['position']['name'] : '' ). $delim .
                        (isset($item['start_date'])       ? $item['start_date']       : '' ). $delim .
                        (isset($item['end_date'])         ? $item['end_date']         : '' );
        }
        return $info;
    }
 
    function getEducationAsString($fbme, $delim = '#', $partDelim = ' | '){
        $info       =   "";
        $flag           =   false;
        if (empty($fbme['education'])) return '';
 
        foreach($fbme['education'] as $item){
            if ($flag)
                $info .= $partDelim;
            $flag    =   true;
            $info   .=  (isset($item['school']['name']) ? $item['school']['name'] : '' ). $delim .
                        (isset($item['year']['name'])   ? $item['year']['name']   : '');
        }
        return $info;
    }
 
    function getArrayDataAsString($data, $delim = '#', $partDelim = ' | '){
        $info       =   "";
        $flag           =   false;
        foreach($data as $item){
            if ($flag)
                $info .= $partDelim;
            $flag    =   true;
            $info   .=  $item['name'];
        }
        return $info;
    }
?>

Recommended Answers

All 3 Replies

If you do something similar to this:

$my_something = new Something();
$my_something->db = new Database_class_thing();

You can than access $this->db from within the class. I.e, it assumes you have a database class that has a function that takes an array and puts it into the database. You could also just write a query there instead and throw that at mysql_query.

i have no idea about coding, could you please show me how "You could also just write a query there instead and throw that at mysql_query"

thank you

Well, there are a lot of tutorials on that on the web, for example: http://devzone.zend.com/node/view/id/641
If you don't want to learn to do this just yet, I'd advice you to use a class for it. I never used one, but this one looks quite okay. It has a query_insert function you can throw that array at.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.