[eluser]Ichabodcole[/eluser]
Hello there, I can't seem to wrap my head around a good and efficient way to join all my tables.
I'm creating a online portfolio and building the backend using codeigniter. The main purpose of the backend is to store and retrieve project information.
In addition to the main 'projects' table I have a 'project_cats' (categories), 'project_clients', 'project_types', 'project_credit_list', and 'project_credits'.
In my database I have a "projects" table, each project needs to have a list of credits like designer, programmer, animator, etc. So i have created a separate table called "project_credits" that contains individual credit types, then I have a third linker table called 'project_credit_list' that stores a "project_id" and a "credit_id" for each row.
So what I don't know how to do is properly get the results of the credits list and join it with my results, and I also don't know how to then properly loop through the credits for each project.
Below is the code I'm using to join all the tables together. It basically returns all projects that match a specific category name.
It all works fine until I get to the credit list and then I'm just failing my arms in the air.
Any help is very appreciated! Thanks.
Code:
function get_projects_by_category($type, $limit=10)
{
/* I first select from the project_cats table getting the category that matches my $type var.*/
$this->db->select('*, project_cats.name AS cat_name');
$this->db->from('project_cats');
$this->db->where('project_cats.name', $type);
/* Now I join the projects table where the projects table cat_id value matches the project_cats table id value.*/
$this->db->join('projects', 'projects.cat_id = project_cats.id', 'left');
/* Now I join the project_types table where the project_types table id value matches the projects table type_id value.*/
$this->db->join('project_types', 'project_types.id = projects.type_id', 'left');
/* Now I join the project_clients table where the project_clients table id value matches the projects table client_id value.*/
$this->db->join('project_clients', 'project_clients.id = projects.client_id', 'left');
/* Now I have no idea how to join the project_credit_list with the project_credits table and then to the projects*/
//$this->db->join('project_credit_list', 'project_credit_list.project_id = projects.id', 'left');
$this->db->order_by('projects.position');
$this->db->limit($limit);
$query = $this->db->get();
return $query->result();
}