Welcome Guest, Not a member yet? Register   Sign In
db table joining issue? or, is there a better way?
#1

[eluser]FireMe[/eluser]
ok, I think I may have a database joining issue or is there a better way or doing what I want, So first let me set the scene. I have a table called quotes, where has 'ID', 'USER_ID', 'FILE_ID' and other fields that are not that important to this.

so on my view I am listing all the quotes for that user id, then I decide I wanted to add a thumbnail image for the file, so i need to somehow get the 'path' field from my file table, so i decide to do the following join?

Code:
function get_all_quotes($user_id){
$this->db->select('*');
$this->db->from('quotes');
$this->db->where('quotes.user_id', $user_id);
$this->db->join('file', 'file.id = quotes.file_id');
$query = $this->db->get();
if($query->num_rows() > 0){
  foreach($query->result() as $row){
   $data[]=$row;
  }
  return $data;
}
else {
}
return false;
    }

which works fine and i get the image thumbnail on the view, but i lose the ID field from the 'QUOTES' table because of the join it overides the ID with the FILE_ID?

print_r bellow

Code:
Array ( [0] => stdClass Object ( [id] => 47 [user_id] => 6 [file_id] => 47 [status] => Quoted [price] => 19.99 [path] => image1.jpg ) [1] => stdClass Object ( [id] => 48 [user_id] => 6 [file_id] => 48 [status] => Quoted [price] => 5.99 [path] => image2.jpg ) )

or is there any easier way than joining the table to get the path field from the file table
#2

[eluser]CroNiX[/eluser]
specify the columns you want to retrieve (instead of *) and alias ones with the same name.
Code:
->select('quotes.file_id AS q_file_id, etc')//now you can access as q_file_id
#3

[eluser]FireMe[/eluser]
Thanks for the reply CroNiX so the best way to do it would be.

Code:
$this->db->select('quotes.id,
                   quotes.user_id,
                   quotes.file_id,
                   quotes.restore_type,
                   quotes.status,
                   quotes.price,
                   quotes.product_type,
                   file.id as f_id,
                   file.path,
                   file.user_id as f_user_id');
#4

[eluser]CroNiX[/eluser]
Yep, now you'd get "id", which would be quotes.id, "f_id" which is file.id, "user_id" which is quotes.user_id and "f_user_id" which is file.user_id, in addition to your unique fields.




Theme © iAndrew 2016 - Forum software by © MyBB