I have three tables in databse "cardetails", "pricedetails" and "stockimages".
I am using join in Codeigniter to get all records form above three tables. I got data from two tables but i am unable to get images data from "stockimages"
Tables details :-
1. Cardetails
carid Make Model Color Body user_id
1 Hundai Varna White Sand 1
2 Audi A5 White Sand 1
3 BMW A3 White Sand 1
4 Audi A1 White Sand 1
2. Pricedetails
priceid carid minPrice MaxPrice
1 3 15000 22000
3. stockimaes
imgid carid imagesurl
1 3 url 1
2 3 url 2
3 3 url 3
4 3 url 4
5 3 url 5
I want to show all cars with price and images in front page.
public function allCarList($userid){
$this->db->select('*');
$this->db->from('cardetails as stock' );
$this->db->where('stock.user_id', $usrid);
$this->db->where('deleteid != ',1);
$this->db->join('price as p', 'stock.carid = p.carid', 'LEFT');
$this->db->order_by('carid','desc');
$q = $this->db->get()->result();
return $q;
}
Unable to get images with all rows related with same carid.
The issue of your code is that you are not joining the other table. You've included only join of these 2 (cardetails
, price
):
$this->db->from('cardetails as stock' );
$this->db->join('price as p', 'stock.carid = p.carid', 'LEFT');
What you need to do is add second join, to connect the third table, generally you can use this syntax:
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id');
$this->db->join('table3', 'table1.id = table3.id');
$query = $this->db->get();
For your example, it would be:
$this->db->from('cardetails as stock' );
$this->db->join('price as p', 'stock.carid = p.carid');
$this->db->join('stockimaes as img', 'stock.carid = img.carid');