Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
are sm_user_profiles and vital_user_profiles correlated by any id? –  Fabio Apr 16 '13 at 5:49
    
The auto incrementing id of vital_user_profiles corresponds to the uid column in sm, although I neglected to mention that. –  Patrick Apr 16 '13 at 22:04
    
i answered, please check and let me know if it works –  Fabio Apr 17 '13 at 6:25

3 Answers 3

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;
share|improve this answer

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.

share|improve this answer

You can use join on

sm_user_profiles.id = vital_user_profiles.id

where

vital_user_profiles.id  =1 ;

something like this

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.