导出时,如何在Excel工作表中显示ID所在的国家/地区名称,州/州和城市名称?

时间:2018-10-19 11:41:24

标签: php mysql codeigniter-3 phpexcel

我在表中有一条记录

Name | Mobile     | Country | State | City
XYZ  | 0123456789 | 231     | 3921  | 42737
mnb  | 0125415245 | 13      | 269   | 6602
lkh  | 9874525415 | 231     | 3939  | 44668

现在我正在使用CodeIgniter。我正在使用PHPExcel将数据导出到excel工作表中

控制器:

public function export_order_list() {
    $object = new PHPExcel();
    $object->setActiveSheetIndex(0);
    $table_columns = array("Name","Mobile","Country","State","City");
    $column = 0;
    $object->getActiveSheet()->getStyle('1:1')->getFont()->setBold(true);

    foreach($table_columns as $field) {
        $object->getActiveSheet()->setCellValueByColumnAndRow($column, 1, $field);
        $column++;
    }
    $export_list = $this->Customer_model->export_order_list_model();//getting all records
    $excel_row = 2;
    foreach($export_list as $row) {
        $object->getActiveSheet()->setCellValueByColumnAndRow(0, $excel_row, $row->Name);
        $object->getActiveSheet()->setCellValueByColumnAndRow(1, $excel_row, $row->Mobile);
        $object->getActiveSheet()->setCellValueByColumnAndRow(2, $excel_row, $row->Country);
        $object->getActiveSheet()->setCellValueByColumnAndRow(3, $excel_row, $row->State);
        $object->getActiveSheet()->setCellValueByColumnAndRow(4, $excel_row, $row->City);
        $excel_row++;
    }

    $object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel5');
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="list.xls"');
    $object_writer->save('php://output');
}

型号:

public function export_order_list_model(){
    $this->db->select("*");
    $this->db->from('tbl_customer');
    $query = $this->db->get();
    $result = $query->result();
    if($result) {
        return $result;
    } else {
        return 0;
    }
}

我正在获取输出。

enter image description here

现在,我的问题是如何显示ID为国家的州名,州名和城市名?

我需要这样的输出

enter image description here

我在数据库中有所有国家,州和城市的名称,表的列名称是

Country
id |country_name

State
id| state_name |country_id

City
id |cities_name|state_id 

更新

请检查我的查询对此输出是否正确?因为它对我不起作用。

$this->db->select("*");
$this->db->from('tbl_customer');
$this->db->join('Country', 'Country.id=tbl_customer.c_b_country');
$this->db->join('State', 'State.id=tbl_customer.c_b_state'); 
$this->db->join('City', 'City.id=tbl_customer.c_b_city');

2 个答案:

答案 0 :(得分:0)

您的原始SQL查询应如下所示:

SELECT 
  cu.name AS Name, 
  cu.mobile AS Mobile,
  c.country_name AS Country
  s.state_name AS State,
  ct.cities_name AS City
FROM tbl_customer cu
  JOIN Country c ON cu.Country = c.id
  JOIN State s ON cu.State = s.id
  JOIN City ct ON cu.City = ct.id;

这里发生了一些事情:

  • 将每个表与您计划使用的数据结合起来
  • 在选择中,您必须引用列在联接表中的列
  • 删除了select *,这最终会在多个地方拉出模糊的字段,例如“ id”
  • 使用AS关键字将列重命名为您在代码中使用的列

尝试直接在数据库中运行此原始查询,以确保其符合您的期望。之后,只是将其格式化回PHP的一种情况。

答案 1 :(得分:0)

此查询将起作用。

$this->db->select("name,mobile,country,state,city");
$this->db->from('customer');
$this->db->join('country', 'country.id=customer.country_id','left');
$this->db->join('state', 'state.id=customer.state_id','left'); 
$this->db->join('city', 'city.id=customer.city_id','left');