Say I have two tables to store user profiles: sm_user_profiles
(social media), and vital_user_profiles
.
Now I want a universal object for accessing profile information. For the sake of clarity, this object is for only one user (the logged-in user) and will not return multiple rows.
Starting with this:
$query = $this->db->query('SELECT * FROM vital_user_profiles WHERE id="1"');
$profile = $query->row();
echo $profile->email;
how can I combine it with this:
$query = $this->db->query('SELECT * FROM sm_user_profiles WHERE id="1"');`
$profile = $query->row();
echo $profile->facebookURL;
so that I can do this?
echo $profile->email.$profile->facebookURL;
I'm new to objects in PHP. Thanks in advance!
You can use join on
sm_user_profiles.id = vital_user_profiles.id
where
vital_user_profiles.id =1 ;
something like this
Yes, you can start creating an object and then in the end, return the object which has all the table's information in it. For example
public function build_user($uid) {
$user_data['vital'] = $this->db->select()->from('vital_user_profiles')->where('id', $uid)->get()->row();
$user_data['sm'] = $this->db->select()->from('sm_user_profiles')->where('id', $uid)->get()->row();
return $user_data;
}
This should give you a nice object which has all the field data in it. And you can keep adding more to it. I have a big model which is doing same kind of thing where I have created a node object from 4 or 5 different tables which is database managed by Drupal and front end is done using Code Igniter. If you want I can send you the code for reference if this doesn't work for you.
you can use LEFT JOIN
function
$query = $this->db->query('SELECT * FROM `vital_user_profiles` a LEFT JOIN `sm_user_profiles` b ON a.`id` = b.`id` WHERE a.`id` = "1"');
$profile = $query->row();
now we have all data we need so you can echo both values
echo 'Email: ' . $profile->email . ' facebook url: ' . $profile->facebookURL;