Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter table join

I want to display both the user table and users_profiles table in 1 table : I want to link them both so that usrpID = usrID,

Before this process I tried displaying only users table using this code and it works great.

Controller:

$data['query'] = $this->db->query('SELECT * FROM users_profiles');
$this->load->view('users/users_view',$data);

View:

<?php foreach($query->result_array() as $row): ?>
        <tr class="even gradeC">
            <td><?php echo $row['usrID']</td>
            <td><?php echo $row['usrName'];?></td>
        </tr>
<? endforeach; ?>

but when I try to join two tables, it returns me an error: this is my code

$this->db->select('users.usrID, users_profiles.usrpID');
$this->db->from('users', 'users_profiles');
$this->db->join('users', 'users.usrID = users_profiles.usrpID');
$result = $this->db->get();

users table has fields like username,password, etc. and every user has his own profile in users_profiles table

users           users_profiles

users tableusers_profiles table

EDIT I tried selecting the fields but when I tried this

<td><?php echo $row['usrID'];?></td>
            <td><?php echo $row['usrName'];?></td>
            <td><?php echo $row['usrpFirstName'].' '.$row['usrpLastName'];?></td>
            <td><?php echo $row['usrpBday'];?></td>
            <td><?php echo $row['usrpSex'];?></td>
            <td><?php echo $row['usrpAddress'];?></td>    

it returns me the first value in users profiles in which it should not

like image 240
kester martinez Avatar asked May 17 '11 00:05

kester martinez


2 Answers

users table was in both from and join functions, so in sum you were joining 3 tables: users, users and users_profiles -> the 2 first have the same name -> error unique/alias table.

Try this (joining [users in from] on [users_profiles in join]):

$this->db->select('users.usrID, users_profiles.usrpID')
         ->from('users')
         ->join('users_profiles', 'users.usrID = users_profiles.usrpID');
$result = $this->db->get();

EDIT:

example:

To get users_profiles userpNick column:

$this->db->select('users.usrID, users_profiles.userpNick')
         ->from('users')
         ->join('users_profiles', 'users.usrID = users_profiles.usrpID');
$query = $this->db->get();

view:

<?php foreach($query->result() as $row): ?>
        <tr class="even gradeC">
            <td><?php echo $row->usrID</td>
            <td><?php echo $row->userpNick;?></td>
        </tr>
<? endforeach; ?>
like image 132
manji Avatar answered Oct 24 '22 01:10

manji


You can refer this example of join result between employee and address table.

function getEmployees(){
  $this->db->select("trn_employee.EMPLOYEE_ID,trn_employee.FIRST_NAME,trn_employee.LAST_NAME,trn_employee.EMAIL,trn_address.ADDRESS_LINE,trn_address.CITY");
  $this->db->from('trn_employee');
  $this->db->join('trn_address', 'trn_address.employee_id = trn_employee.employee_id');
  $query = $this->db->get();
  return $query->result();
 }
like image 42
Vicky Avatar answered Oct 23 '22 23:10

Vicky