php - Join Tables in codeigniter -
i have 2 tables: company & users.
i have form in insert company name , others details. in same form have sub form sales info , tech info.
the data insert in sales , tech info gets stored users tables.and id stored company table in 2 fields called sales_id , tech_id.
now view want fetch company name sales person , tech person,how it?
the code in model:
public function get_company() { $this->db->select('*'); $this->db->join('users','users.id = company.sales_id','users.id = company.tech_id'); $this->db->from('company'); $query = $this->db->get(); $result = $query->result(); return $result; }
in view:
<?php if(count($companys)): foreach($companys $company): ?> <td><?php echo $company->first_name; ?></td>
how differentiate sales , tech person?
the controller:
public function add_company($id = null) { $this->data['company'] = $this->company_m->get_new(); $this->data['user'] = $this->user_m->get_new(); $rules = $this->company_m->rules_admin; $this->form_validation->set_rules($rules); if ($this->form_validation->run() == true) { /*inserting sales person information*/ $data['first_name'] = $this->input->post('first_name_s'); $data['last_name'] = $this->input->post('last_name_s'); $data['email'] = $this->input->post('email_s'); $data['user_type'] ="sales"; $this->user_m->save($data,$id); $sales_id = $this->db->insert_id(); /*inserting tech person information*/ $data_tech =$this->user_m->array_from_post(array('first_name','last_name','email')); $this->user_m->save($data_tech,$id); $tech_id = $this->db->insert_id(); /*insert company information*/ $data = $this->company_m->array_from_post(array('org_name','dba','addr1','addr2','city','state','country','pin','sales_id','tech_id','tax_number','comment','url')); $data['sales_id']= $sales_id; $data['tech_id']= $tech_id; $org_id = $this->company_m->save($data, $id); redirect('admin/company'); } // load view $this->data['subview'] = 'admin/company/add'; $this->load->view('admin/_layout_main', $this->data); }
i hope understood issue.
the users table
the view code:
<table class="table table-striped "> <thead> <tr class="warning"> <th>organization name</th> <th>sales person</th> <th>tech person</th> <th>tax no</th> <th>edit</th> <th>delete</th> </tr> </thead> <tbody> <?php if(count($companys)): foreach($companys $company): ?> <tr class="active"> <td contenteditable="true" onclick="edit_company(this)" onblur="save_company(this,'org_name','<?php echo $company->id; ?>')" ><?php echo $company->org_name; ?></td> <td contenteditable="true" onclick="edit_company(this)" onblur="save_company(this,'sales_id','<?php echo $company->id; ?>')" ><?php echo $company->first_name; ?></td> <td contenteditable="true" onclick="edit_company(this)" onblur="save_company(this,'tech_id','<?php echo $company->id; ?>')" ><?php echo $company->first_name; ?></td> <td contenteditable="true" onclick="edit_company(this)" onblur="save_company(this,'tax_number','<?php echo $company->id; ?>')" ><?php echo $company->tax_number; ?></td> <td><?php echo btn_edit('admin/company/edit/' . $company->id); ?></td> <td><?php echo btn_delete('admin/company/delete/' . $company->id); ?></td> </tr> <?php endforeach; ?> <?php else: ?> <tr> <td colspan="3">we not find users.</td> </tr> <?php endif; ?> </tbody> </table>
in model use
public function get_company() { $this->db->select('*'); $this->db->from('company'); $query = $this->db->get(); $result = $query->result(); return $result; }
for view :-
<?php if(count($companys)): foreach($companys $company): $tech_person = $this->db->get_where("users",array("id"=>$company->tech_id))->row(); $sales_person = $this->db->get_where("users",array("id"=>$company->sales_id))->row(); ?> <tr class="active"> <td contenteditable="true" onclick="edit_company(this)" onblur="save_company(this,'org_name','<?php echo $company->id; ?>')" ><?php echo $company->org_name; ?></td> <td contenteditable="true" onclick="edit_company(this)" onblur="save_company(this,'sales_id','<?php echo $company->id; ?>')" ><?php echo $tech_person->first_name; ?></td> <td contenteditable="true" onclick="edit_company(this)" onblur="save_company(this,'tech_id','<?php echo $company->id; ?>')" ><?php echo $sales_person->first_name; ?></td> <td contenteditable="true" onclick="edit_company(this)" onblur="save_company(this,'tax_number','<?php echo $company->id; ?>')" ><?php echo $company->tax_number; ?></td> <td><?php echo btn_edit('admin/company/edit/' . $company->id); ?></td> <td><?php echo btn_delete('admin/company/delete/' . $company->id); ?></td> </tr> <?php endforeach; ?> <?php else: ?> <tr> <td colspan="3">we not find users.</td> </tr> <?php endif; ?> </tbody>
in method not need join tech , sales person. edited according view
Comments
Post a Comment