I am using codeigniter and I am trying to call 3 different tables and join them together into one array. This is what I have:
$this->db->select('so_alcohol.id,
so_alcohol.name as name,
so_alcohol.category as category,
so_alcohol.permName as permName,
so_alcohol.picture as alcoholPicture,
group_concat(so_user.firstName) as firstName,
group_concat(so_user.lastName) as lastName,
group_concat(so_user.picture) as userPicture,
group_concat(so_rating.idUser) as idUser,
group_concat(so_rating.overall) as overall,
group_concat(so_rating.description) as description');
$this->db->from('alcohol');
$this->db->order_by("id", "desc");
$this->db->join('so_rating', 'so_rating.idAlcohol = so_alcohol.id', 'LEFT');
$this->db->join('so_user', 'so_user.id = so_rating.idUser', 'LEFT');
$query = $this->db->get();
The output I would like is the following:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Product Name
[category] => Product category
[permName] => Product-Name
[alcoholPicture] => http://image.com/1.jpg
[idUser] => 1,2,3
[overall] => 100,80,50
[description] => Review 1,Awesome review 2, Horrible review 3
[firstName] => name 1, name 2, name 3
[lastName] => last 1, last 2, last 3
[userPicture] => http://userimage.com/1.jpg, http://userimage.com/2.jpg, http://userimage.com/3.jpg
)
[0] => stdClass Object
(
[id] => 1
[name] => Product Name
[category] => Product category
[permName] => Product-Name
[alcoholPicture] => http://image.com/1.jpg
[idUser] => 1,2,3
[overall] => 100,80,50
[description] => Review 1,Awesome review 2, Horrible review 3
[firstName] => name 1, name 2, name 3
[lastName] => last 1, last 2, last 3
[userPicture] => http://userimage.com/1.jpg, http://userimage.com/2.jpg, http://userimage.com/3.jpg
)
)
My question is how do I make what I have work, right now there are two problems (that I have found) First it only outputs 1 result, I need it to output 40 and second I cant so_user.id = so_rating.idUser
because I already group_concat(so_rating.idUser)
Any Ideas? I am not even sure if this is the right way of doing it as I am still a php newbie.
EDIT:
Alcohol
id name category permName picture
1 Product #1 1 Product-1 http://someurl.com/1.jpg
2 Product #2 1 Product-2 http://someurl.com/2.jpg
3 Product #3 1 Product-2 http://someurl.com/3.jpg
4 Product #4 2 Product-2 http://someurl.com/4.jpg
Rating
id idUser idProduct overall description
1 1 2 100 Great Product Review 1
2 2 2 75 Great Product Review 2
3 1 3 100 Great Product Review 3
4 2 3 98 Great Product Review 4
User
id firstName lastName userPicture
1 first 1 last 1 http://someurlUserPicture.com/1.jpg
2 first 2 last 2 http://someurlUserPicture.com/2.jpg
3 first 3 last 3 http://someurlUserPicture.com/3.jpg
4 first 4 last 4 http://someurlUserPicture.com/4.jpg