我有以下查询...
public function get_invoice_user_summary_csv($invoice_table_id)
{
$sql_get_user_summ = "
SELECT `billingHistory`.`$invoice_table_id`.userId as userId, ipCore.users.firstname, ipCore.users.surname, COUNT(`billingHistory`.`$invoice_table_id`.id) AS totalNum, SUM(`billingHistory`.`$invoice_table_id`.durationSeconds) AS totalDuration, (SUM(`billingHistory`.`$invoice_table_id`.priceMin)+SUM(`billingHistory`.`$invoice_table_id`.priceCon)) AS totalCost
FROM `billingHistory`.`$invoice_table_id`
LEFT JOIN ipCore.users ON `billingHistory`.`$invoice_table_id`.userId = ipCore.users.id
GROUP BY userId
ORDER BY ipCore.users.surname ASC, ipCore.users.firstname ASC;";
$query = $this->db_mtvm->query($sql_get_user_summ);
if($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$line = '';
$value = '';
foreach($row as $value)
{
if((!isset($value)) || ($value == ""))
{
$value = ",";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . ",";
}
$line .= $value;
}
print substr(str_replace("\r", "", trim($line)), 0, -1)."\r\n";
flush();
ob_flush();
}
}
else return false;
}
这个返回格式的 csv 数组放在不同的控制器中。
然而,它只是数据的主体(正如我所料)。但是,我已经阅读了一些我认为类似的问题,但在这个示例中我需要放置列标题的地方并不能完全解决。要么手动输入,要么我将从 sql 查询中分配这些。