如何将多个参数传递给codeigniter中的函数或查询

时间:2015-04-13 11:27:27

标签: php mysql codeigniter

我想要的是获取所有客户的个人资料,其地址是印度。

我有两张表customersaddress。 ' customers'表格有address_ID,而address表格中有country_ID

我到目前为止尝试过这个:

在ajax调用中,我得到了这个控制器的功能:

function listCustomerByCountry(){
if(isset($_REQUEST['id']) && $_REQUEST['id']!=null)
{       $id=$_REQUEST['id'];
        //print_r($id);

         $cid=$this->report_model->getAddressidByCountryid($id);

        foreach($cid as $rs)
        {
            $addid = $rs['addressId'];  
            print_r($addid);
            $result=$this->report_model->get_customer_by_addressId($addid);
            print_r($result);
        }

         //var_dump($cid);
         //die();
            $result=$this->report_model->get_customer_by_addressId($addid);
            print_r($result);


}else
{
        $result=$this->report_model->get_customer_by_addressId();
}
echo'<table class="table table-striped table-bordered table-hover dataTables-example" ><thead><tr><th>Id</th><th>Customer Name</th><th>Customer Address</th><th>Phone Number</th><th>Email</th><th>Plan Type</th><th>Application Status</th><th>View</th><th>Edit</th><th>Ticket</th></tr></thead><tbody>';
foreach ($result as $customers){
$status = unserialize (STATUS);
$statusDb = $customers['applicationStatus'];

        $val='<tr class="gradeX"><td>'.$customers['customerVisibleId'].'</td>';
        $val.='<td><a href="'.base_url("customer/profile/".$customers['customerId']).'">'.$customers['customerName'].'</td>';
        $val.='<td>'.$customers['customerAddress'].'</td>';
        $val.='<td>'.$customers['phnoMobile'].'</td>';
        $val.='<td>'.$customers['email1' ].'</td>';
        $val.='<td>'.$customers['planId'].'</td>';
        $val.='<td>'.($status[$statusDb]).'</td>';
        $val.='<td><a href="'. base_url("customer/view/".$customers['customerId']).'"><i class="fa fa-eye"></i></a></td>';
        $val.='<td><a href="'.base_url("customer/edit/".$customers['customerId']).'"><img src="'.base_url().'assets/img/edit.png" width="16" height="16" /></a></td>';
        $val.='<td><a href="'.base_url("customer/ticketid/".$customers['customerId']).'"><i class="fa fa-text-width"></i></a></td>';               
        $val.= '</tr>';
        echo $val;

}
echo '</tbody><tfoot></tfoot></table>';
die();

}

在模型中,我根据下拉选项从country_ID表中获取address,然后根据customersaddress_ID表中获取客户的个人资料数据}。

型号:

    public function getAddressidByCountryid($id){
$query = $this->db->get_where('address', array('country' => $id));
$row=$query->result_array();
return $row;

}

public function get_customer_by_addressId($id)
{
$query = $this->db->get_where('customers', array('customerAddress' => $id));
print_r($id);
die();
return $query->result_array();
}

问题是我从第二个函数get_customer_by_addressId

获取时只获取最后一个address_ID和最后一个customerID

我在做什么错?控制器功能不通过所有addres_IDs?

表格结构

Table Customers
+----+-------+-----------+
| id | name  | addressID |
+----+-------+-----------+
|  1 | demo  |        12 |
|  2 | test  |        13 |
|  3 | demo1 |        14 |
|  4 | demo2 |        15 |
+----+-------+-----------+

Table address:

+-----------+-----------+------------------+-----+
| addressID | countryID | stateID          | cityID |
+-----------+-----------+------------------+-----+
|        12 |         1 |                1 |   1 |
|        13 |         1 |                1 |   1 |
|        14 |         1 |                1 |   1 |
|        15 |         2 |                2 |   2 |
+-----------+-----------+------------------+-----+

结果表

+----+-------+-----------+
| id | name  | addressID |
+----+-------+-----------+
|  1 | demo  |        12 |
|  2 | test  |        13 |
|  3 | demo1 |        14 |
+----+-------+-----------+

现在我想要所有自定义数据的国家/地区ID为1

的数据

1 个答案:

答案 0 :(得分:0)

所以我正在更新您的代码,如下所示。

<强>控制器

$result = $this->report_model->getAddressidByCountryid($id);
print_r($result);

并在您的模型中

public function getAddressidByCountryid($id){
  $this->db->select('c.*');
  $this->db->from('Customers c');
  $this->db->join('address a','c.addressID = a.addressID','left');
  $this->db->where('a.countryID',$id);
  $result = $this->db->get()->result_array();
  return $result;
}