使用 Codeigniter 生成带有 2 个表的 CSV

Posted

技术标签:

【中文标题】使用 Codeigniter 生成带有 2 个表的 CSV【英文标题】:Generate CSV with 2 Tables using Codigniter 【发布时间】:2018-06-09 14:22:58 【问题描述】:

我在数据库中有两个表。一个是潜在客户和其他潜在客户备注。潜在客户和潜在客户备注具有外键关系。我想生成一个 CSV,因此它有一个潜在客户列,然后是与该潜在客户相关的所有备注。

Here is what I need 我当前的代码是

    $all_leads = '';
    $name = 'leads-'.date('d-m-Y').'.csv';
    $this->load->dbutil();
    $this->db->select("lead_id as Lead ID,business_name as Business,contact_name as First Name,contact_name_last as Last Name,work_email as Email,contact_email as Personal Email,work_number as Work Number,cell_no as Cell No,city as City,state as State,zip as Zip,date_added as Date Added,IF( label_type <> 0,'Important',' ') as Label, companies.company_name as company name");
    $this->db->from('leads');
    $this->db->join('companies', 'companies.id = leads.company');
    $leads = $this->db->get();
    $num_rows = $leads->num_rows();

    $all_leads= $this->dbutil->csv_from_result( $leads );
    write_file( $this->file_path . '/'.$name,$all_leads );
    $data = file_get_contents($this->file_path . '/'.$name);

    force_download( $name, $data );
    delete_files( $this->file_path . '/'.$name );`

【问题讨论】:

【参考方案1】:

我将分享一段可能对您有所帮助的代码。您所要做的就是根据您的要求调整数据。请检查下面的代码

<?php
    $this->db->select("a.lead_id,a.business_name,a.a.contact_name_first,a.contact_name_last,a.work_email,a.contact_email,a.work_number,a.cell_no,a.city,a.state,a.zip,a.date_added,IF( a.label_type <> 0,'Important',' ') as label, b.company_name");
    $this->db->from('leads a');
    $this->db->join('companies b', 'b.id = a.company');
    $this->db->join('lead_note c', 'c.lead_id = a.id');

    $query = $this->db->get();
    $result = $query->result_array();

    if($query->num_rows() > 0)
        $delimiter = ",";
        $filename = "members_" . date('Y-m-d') . ".csv";

        //create a file pointer
        $f = fopen('php://memory', 'w');

        //set column headers
        $fields = array('Lead ID','Business','First Name','Last Name','Email','Personal Email','Work Number','Cell No','City','State','Zip','Date Added');

        //output each row of the data, format line as csv and write to file pointer
        foreach($result as $row)

            $lineData = array($row['lead_id'], $row['contact_name_first'], $row['contact_name_last'], $row['work_email'], $row['contact_email'], $row['work_number'], $row['cell_no'], $row['city'], $row['state'], $row['zip'], $row['date_added'], $row['label'], $row['company_name']);
            fputcsv($f, $lineData, $delimiter);
        

        //move back to beginning of file
        fseek($f, 0);

        //set headers to download file rather than displayed
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename="' . $filename . '";');

        //output all remaining data on a file pointer
        fpassthru($f);
    
    exit;

    ?>

希望这会有所帮助。不要在别名和表名中使用空格。如果使用空格,则需要正确使用反引号。我强烈建议为表和别名使用驼峰式或 undersoce 命名约定

【讨论】:

这只有一个表数据。如何获取此代码的两个表数据? 有帮助吗? 请分享完整架构。我对您的数据没有完全了解。对于连接,我需要有关整个表的详细信息。 查看最新编辑。请告诉我它有帮助 我已经尝试过加入,但它没有放在新行上。它也重复了主角【参考方案2】:

感谢大家的帮助..我是这样做的..

check_admin();
    header('Content-Type: text/csv; charset=utf-8');  
    $name = 'leads-'.date('d-m-Y').'.csv';
    header('Content-Disposition: attachment; filename='.$name.'');  
    $output = fopen("php://output", "w");  
    fputcsv($output, array('Lead ID', 'Business', 'First Name' ,'Last Name', 'Email','Work Number','Cell No','City','State','Zip','Date Added', 'Label','company name','Business', 'First Name' ,'Last Name', 'Email','Work Number','Cell No'));  
    $query = "SELECT leads.`lead_id`, leads.`business_name` as `Business`, leads.`contact_name` as `First Name`, leads.`contact_name_last` as `Last Name`, leads.`work_email` as `Email`, leads.`work_number` as `Work Number`, leads.`cell_no` as `Cell No`, `city` as `City`, `state` as `State`, `zip` as `Zip`, `date_added` as `Date Added`, IF( label_type <> 0, 'Important', ' ') as Label, `companies`.`company_name` as `company name`,lead_contact.`company_name` as `Business1`, lead_contact.`contact_name` as `First Name1`, lead_contact.`contact_name_last` as `Last Name1`, lead_contact.`work_email` as `Email1`, lead_contact.`work_number` as `Work Number1`, lead_contact.`cell_no` as `Cell No1` FROM `leads` Left JOIN `companies` ON `companies`.`id` = `leads`.`company` Left JOIN `lead_contact` ON `lead_contact`.`lead_id` = `leads`.`lead_id`"; 
    $all_leads = $this->db->query( $query ); 
    foreach ($all_leads->result_array( ) as $lead)
        $lead_id = $lead['lead_id'];
        $query = "SELECT `note_date`,`notes` from lead_notes where lead_id='$lead_id'";
        $leads_notes = $this->db->query($query);
        fputcsv($output, $lead);
        //fputcsv($output, array('','Lead ID', 'Added', 'Notes'));
    foreach ($leads_notes->result_array( ) as $leads_note)

        fputcsv($output, $leads_note);

    


【讨论】:

以上是关于使用 Codeigniter 生成带有 2 个表的 CSV的主要内容,如果未能解决你的问题,请参考以下文章

基于codeigniter中2个表的id显示数据

带有 2 个表的 SQL Server GROUP BY

是否可以使用带有 Codeigniter 的 Datamapper 来获取表的字段名称?

如何使用 Codeigniter 3 一次将图像输入到 2 个表中

加入 2 个表并在 codeigniter 中对条件求和

Postgresql - 带有 x 个表的转储数据库 - 仅模式,但来自一个表的数据