在codeigniter

时间:2016-02-08 08:48:47

标签: php mysql codeigniter

我有两张桌子:公司&的用户

我有一个表格,其中我插入公司名称和其他详细信息。在同一形式我有一个子表单销售信息和技术信息。

我在销售和技术信息中插入的数据存储在用户表中。他们的ID存储在公司表中的两个名为sales_id和tech_id的字段中。

现在我希望获取公司名称的销售人员及其技术人员,如何做到这一点?

模型中的代码:

        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; 

    }

在视图中:

<?php if(count($companys)): foreach($companys as $company): ?>  

  <td><?php echo $company->first_name; ?></td>

如何区分哪个是销售人员,哪个是技术人员?

控制器:

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 the view
        $this->data['subview'] = 'admin/company/add';
        $this->load->view('admin/_layout_main', $this->data);

    }

我希望你理解我的问题。

公司表 company tabb;e

用户表

users

查看代码:

<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 as $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 could not find any users.</td>
            </tr>
    <?php endif; ?> 
            </tbody>
        </table>

2 个答案:

答案 0 :(得分:1)

在模型中使用

  public function get_company()
    {

        $this->db->select('*');
        $this->db->from('company');
        $query = $this->db->get();
        $result = $query->result(); 
        return $result; 

    }

查看: -

 <?php if(count($companys)): foreach($companys as $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 could not find any users.</td>
        </tr>
<?php endif; ?> 
        </tbody>

在这种方法中,您甚至不需要加入技术和销售人员。 根据您的观点编辑

答案 1 :(得分:0)

首先将控制器中的代码移动到模型中。如果您使用MVC框架尝试使用它来构建它,它将有助于保持一切清洁。

所以现在在模型中你应该有以下内容:

function getCompany(){
    $query = $this->db->select('users.first_name, company.org_name')
    ->from('company')
    ->join('users','users.id = company.sales_id','users.id = company.tech_id')
    ->get()
    ->result_array(); 
    return $query; 
}

在您的视图中,您将执行以下操作:

<?php if(count($companys)): foreach($companys as $company): ?>  
<tr>
    <td><?php echo $company['first_name']; ?></td>
    <td><?php echo $company['org_name']; ?></td>
</tr>
<?php endforeach; endif;?>