for each loop PHP Facebook

Kevin van Werkhooven

I try to make a web application for my thesis. Actually it is almost finished but I still have to find a way to store the likes of users in a database. I collect user information and try to see if there is a relation between movies that he or she like and user data elements that are saved in different SNSs. For single information it already save the things the way it should be. The problem is, that facebook work with arrays and objects and I am not able to find a way to save all the likes in a single string (and then save it in a database).

// Insert or update user data to the database
$fbUserData = array(
    'oauth_provider'        => 'facebook',

    //basic profile
    'oauth_uid'             => $fbUserProfile['id'],
    'first_name'            => $fbUserProfile['first_name'],
    'last_name'             => $fbUserProfile['last_name'],
    'picture'               => $fbUserProfile['picture']['url'],
    'email'                 => $fbUserProfile['email'],
    'link'                  => $fbUserProfile['link'],
    'age_range'             => $fbUserProfile['age_range']['min'],
    'currency'              => $fbUserProfile['currency']['user_currency'],
    'devices'               => $fbUserProfile['devices'],//['hardware']
    'gender'                => $fbUserProfile['gender'],
    'install_type'          => $fbUserProfile['install_type'],
    'installed'             => $fbUserProfile['installed'],
    'is_shared_login'       => $fbUserProfile['is_shared_login'],
    'is_verified'           => $fbUserProfile['is_verified'],
    'locale'                => $fbUserProfile['locale'],
    'name_format'           => $fbUserProfile['name_format'],
    'payment_pricepoints'   => $fbUserProfile['payment_pricepoints'], //credits?
    'security_settings'     => $fbUserProfile['security_settings']['secure_browsing']['enabled'],
    'test_group'            => $fbUserProfile['test_group'],
    'timezone'              => $fbUserProfile['timezone'],
    'updated_time'          => $fbUserProfile['updated_time'],
    'verified'              => $fbUserProfile['verified'],
    'video_upload_limits'   => $fbUserProfile['video_upload_limits']['size'],
    'viewer_can_send_gift'  => $fbUserProfile['viewer_can_send_gift'],

    //extended permissions
    'relationship_status'   => $fbUserProfile['relationship_status'],
    'hometown'              => $fbUserProfile['hometown']['name'],
    'languages'             => $fbUserProfile['languages'][0]['name'].",".$fbUserProfile['languages'][1]['name'].",".$fbUserProfile['languages'][2]['name'].",".$fbUserProfile['languages'][3]['name'].",".$fbUserProfile['languages'][4]['name'],
    //'likes'               => $fbUserProfile['likes'][0]['name'].",".$fbUserProfile['likes'][1]['name']
    'favorite_athletes'     => $fbUserProfile['favorite_athletes'][0]['name'].",".$fbUserProfile['favorite_athletes'][1]['name'].",".$fbUserProfile['favorite_athletes'][2]['name'].",".$fbUserProfile['favorite_athletes'][3]['name'].",".$fbUserProfile['favorite_athletes'][4]['name'].",".$fbUserProfile['favorite_athletes'][5]['name'].",".$fbUserProfile['favorite_athletes'][6]['name'],

);
$userData = $user->checkUser($fbUserData);

//print
echo $userData['languages'];
echo $userData['favorite_athletes'];

As you can see for languages, likes, favorite_athletes I coded it in the ugly way now to read the profile and store it. The problem is that some people have over 100 likes, and it is of course not handy to code it in this way. When I use the for each it always bug...

TommyBs

If you're just interested in concatenating arrays for a single table then use PHPs implode function http://php.net/manual/en/function.implode.php

combined with an array map as mentioned here

How to use implode on an array of stdClass objects?

alternatively normalise your database as mentioned in a comment

    'favorite_athletes'     => implode(",", array_map(function($x) { return $x['name'];},$fbUserProfile['favorite_athletes']));
    'likes'               => implode(",", array_map(function($x){ return $x['name']}, $fbUserProfile['likes']['data']));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related