我在DB中有两个表。一个是Lead和其他主要说明.. Lead和Lead note有外键关系..我想生成一个CSV,所以它有一个引导列然后所有与该Lead相关的注释..
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 );`
我将分享一段可能对您有帮助的代码。您需要做的是根据您的要求调整数据。请检查以下代码
<?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;
?>
希望这可以帮助。不要使用空格来表示别名和表名。如果使用空格,则需要正确使用后退。我强烈建议使用camelcase或undersoce命名对齐表格和别名
谢谢大家的帮助......我这样做了..
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);
}
}