2016-04-29 50 views
1

我想获取特定公司的客户,其中company_id是会话变量。我将发布我的控制器的代码在数据被提取的地方。目前,所有的客户都被抓取,而不是特定公司的客户。Codeigniter:基于特定公司ID获取客户

CODE:

public function index() 
{ 
if($this->input->post("export_btn")){ 
    $type = $this->input->post("type"); 
    $sel_elements = $this->input->post("selectItemsel_elements"); 

    // filename for download 
    if("customer" == $type){ 
     $adco = $this->session->userdata('company_id'); 
     $this->db->select("naturescrm_customers.* FROM naturescrm_customers"); 
     $this->db->join("naturescrm_company","naturescrm_company.company_id=naturescrm_customers.company_id AND naturescrm_customers.company_id='".$adco."'","left"); 
     $customers = $this->customer_m->get_by("id IN (".implode(",",$sel_elements).")"); 
     foreach($customers as $order){ 
      $data[] = array(
       "customer_id" =>"".$order->customer_id, 
       "first name" =>$order->name, 
       "last name" =>$order->lname, 
       "address 1" =>$order->address, 
       "address 2" =>$order->add2, 
       "city" =>$order->city, 
       "state" =>$order->state, 
       "country" =>$order->country, 
       "zip_code" =>$order->zip_code, 
       "email" =>$order->email, 
       "phone" =>$order->phone, 
      ); 
     } 
} 

同样在下面的代码,我怎么让客户出现基于该公司session变量来传递会话变量?

CODE

if("customer" == $type){ 
    $elements = array(); 
    $adco = $this->session->userdata('company_id'); 
    $customer = $this->customer_m->get(); 
    foreach ($customer as $v) { 
     $elements[ $v->id] = $v->customer_id .'-'.$v->name; 
    } 
    $this->data['elements'] = $elements; 

} 

任何帮助将gratly赞赏。谢谢

+0

附加where条件加入后'$这个 - > DB->其中( “naturescrm_company.company_id”,$ ADCO)' –

+0

好吧,我会尝试你的代码。在这种情况下,我是否删除了连接中的AND? –

+0

如果你目前获取所有记录然后,不只是尝试只添加条件:) –

回答

1

尝试增加过滤器一样

$this->db->select("naturescrm_customers.*"); 
$customer = $this->customer_m->get(); 
foreach ($customer as $v) { 
    if($adco === $v->company_id){ 
      $elements[ $v->id] = $v->customer_id .'-'.$v->name; 
    } 
} 
+0

这工作。谢谢 :) –