【问题标题】:How to add only some columns from query in CodeIgniter如何在 CodeIgniter 中仅添加查询中的某些列
【发布时间】:2016-04-05 06:16:58
【问题描述】:

我正在尝试将数据导出到 CSV。我只想要 CodeIgniter 中的 CSV 中的一些字段,但它说“您必须提交一个有效的结果对象”。我的代码做错了吗?

这是我尝试过的代码:

    $projectdetails = $this->db->query('select hs_hr_employee.emp_number,hs_hr_employee.emp_firstname,hs_hr_employee.emp_middle_name,hs_hr_employee.emp_lastname,ohrm_customer.customer_id,ohrm_customer.name as customername,ohrm_project.project_id,ohrm_project.customer_id,ohrm_project.name as projectname,
                ohrm_project_admin.*, hs_hr_employee.*
                from hs_hr_employee,ohrm_customer,ohrm_project,
                ohrm_project_admin where 
                ohrm_project_admin.project_id=ohrm_project.project_id and 
                ohrm_project.customer_id=ohrm_customer.customer_id  and ohrm_project_admin.project_id=ohrm_project.project_id and ohrm_project_admin.emp_number=hs_hr_employee.emp_number
                ');


    $this->load->dbutil();
    $this->load->helper('download');
    $delimiter = ",";
    $newline = "\r\n";
    foreach($projectdetails->result() as $projectdetails12)
    {
        $projectname=$projectdetails12->projectname;
        $customername=$projectdetails12->customername;
        $projectadmin=$projectdetails12->emp_firstname.$projectdetails12->emp_middle_name.$projectdetails12->emp_lastname;
        $downloadprojectdetails=array('projectname'=>$projectname,'customername'=>$customername,'projectadmin'=>$projectadmin);

        $filename = 'projectdetails.csv';
        $data = $this->dbutil->csv_from_result($downloadprojectdetails, $delimiter, $newline);
        force_download($filename, $data);
    }

output using print_r
Array ( [0] => stdClass Object ( [emp_number] => 3 [emp_firstname] => Suresh [emp_middle_name] => [emp_lastname] => Katiki [customer_id] => 1 [customername] => suresh [project_id] => 2 [projectname] => fff) [1] => stdClass Object ( [emp_number] => 2 [emp_firstname] => praveen [emp_middle_name] => [emp_lastname] => reddy [customer_id] => 2 [customername] => praveen [project_id] => 3 [projectname] => ota ) [2] => stdClass Object ( [emp_number] => 1 [emp_firstname] => chandra [emp_middle_name] => [emp_lastname] => kanth [customer_id] => 3 [customername] => chandra [project_id] => 1 [projectname] => orangehrm ) [3] => stdClass Object ( [emp_number] => 4 [emp_firstname] => aashish [emp_middle_name] => [emp_lastname] => madiri [customer_id] => 4 [customername] => aashish [project_id] => 4 [projectname] => test) )

【问题讨论】:

  • 显示$this->dbutil->csv_from_result()的代码

标签: php codeigniter csv


【解决方案1】:

$this->dbutil->csv_from_result 只需要查询对象而不需要结果对象。 在这里你必须传递 $this->dbutil->csv_from_result($projectdetails,$delimiter, $newline); 而不是任何数组或结果对象

试试下面的代码

$projectdetails = $this->db->query('select ohrm_project.name as projectname,
concat(hs_hr_employee.emp_firstname," ",hs_hr_employee.emp_middle_name," ", hs_hr_employee.emp_lastname) as projectadmin,
 ohrm_customer.name as customername
            from hs_hr_employee,ohrm_customer,ohrm_project,
            ohrm_project_admin where 
            ohrm_project_admin.project_id=ohrm_project.project_id and 
            ohrm_project.customer_id=ohrm_customer.customer_id  and ohrm_project_admin.project_id=ohrm_project.project_id and ohrm_project_admin.emp_number=hs_hr_employee.emp_number
            ');


$this->load->dbutil();
$this->load->helper('download');
$this->load->helper('file');
$delimiter = ",";
$newline = "\r\n";
$filename = 'projectdetails.csv';
if($projectdetails->num_rows() >0)
{
    $data = $this->dbutil->csv_from_result($projectdetails, $delimiter, $newline);
    force_download($filename, $data);
}

【讨论】:

  • 不工作它的说法你必须提交一个有效的结果对象
  • 您的查询是否返回任何内容
  • 它显示这个错误你必须提交一个有效的结果对象
  • 还有其他下载csv数据的方法吗?
  • 太棒了!它起作用了你做了什么?你能解释一下吗
猜你喜欢
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多