我在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 );`
答案 0 :(得分:0)
我将分享一段可能对您有帮助的代码。您需要做的是根据您的要求调整数据。请检查以下代码
step2
希望这会有所帮助。不要使用空格来表示别名和表名。如果使用空格,则需要正确使用后退。我强烈推荐用于表格和别名的camelcase或undersoce命名对齐
答案 1 :(得分:0)
感谢大家的帮助......我这样做..
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);
}
}