-1

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...

3
  • you could use PHPs implode function if you just want to concatenate an array Commented Jul 12, 2017 at 10:15
  • 1
    You should not save all likes with names as a long string in a single database field. Instead normalize your database, use a different table for the like information and a separate table linking the two. The same applies to all the other concatenated values. Commented Jul 12, 2017 at 10:16
  • it always bug. What does that actually mean Commented Jul 12, 2017 at 10:16

2 Answers 2

0

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']));
Sign up to request clarification or add additional context in comments.

Comments

0
// 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'][0]['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'             => implode(",", array_map(function($x) { return $x['name'];},$fbUserProfile['languages'])),//$fbUserProfile['languages'][0]['name'].",".$fbUserProfile['languages'][1]['name'].",".$fbUserProfile['languages'][2]['name'].",".$fbUserProfile['languages'][3]['name'].",".$fbUserProfile['languages'][4]['name'],
    'likes'               => implode(",", array_map(function($x){ return $x['name'];}, $fbUserProfile['likes']['data'])),
    'favorite_athletes'     => implode(",", array_map(function($x) { return $x['name'];},$fbUserProfile['favorite_athletes']))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.