[英]Export column title in csv
I have to improve this code for a better output on my CSV file.我必须改进此代码才能在我的 CSV 文件中获得更好的输出。 What I need is to export column title too and maybe replace the native column title witha custom values.
我需要的是也导出列标题,并可能用自定义值替换本机列标题。 Also in csv i have some prices in this form "75.0000", i don't want all that ZERO in the output
同样在 csv 中,我有一些“75.0000”形式的价格,我不希望输出中的所有零
<?php
$db = new mysqli("server","user","password","db");
$fp = fopen("list.csv","w");
if ($rs = $db->query("SELECT column, price, id FROM table,table1 WHERE table.id=table1.id AND table2.id=table.id AND GROUP BY table2.id"))
{
while ($row = $rs->fetch_assoc())
{
fputcsv($fp, array_values($row));
}
$rs->close();
}
fclose($fp);
?>
do you have any suggestion?你有什么建议吗? Thanks a lot!
非常感谢!
I've just edited this part of the code我刚刚编辑了这部分代码
{
while ($row = $rs->fetch_assoc())
{
fputcsv($fp, array_values($map));
fputcsv($fp, array_values($row));
}
But Now, in the CSV file I have each odd line with the array that I called $map... Of course, I need it just one time as Header Thank You但是现在,在 CSV 文件中,我将每个奇数行与我称为 $map 的数组...当然,我只需要一次作为标题谢谢
I Solved changing the php like this:我解决了像这样更改php:
<?php
include_once('connection.php');
$db = new mysqli("SERVER","USER","PASS","DB");
$date = date('d-M-Y-h-i-s');
$fp = fopen("lista-ordini-$date.csv","w");
$filename = ("lista-ordini-$date.csv");
$map = array("Num Ordine", "Nome", "Data", "Stato", "Prodotto", "SKU", "Prezzo base", "IVA", "Sconto", "Prezzo + IVA", "Totale", "ID");
$query = "SELECT sales_flat_order.increment_id, sales_flat_order_grid.billing_name, sales_flat_order_grid.created_at, sales_flat_order_grid.status, sales_flat_order_item.name, sales_flat_order_item.sku, sales_flat_order_item.base_price,sales_flat_order_item.tax_amount, sales_flat_order_item.discount_amount, sales_flat_order_item.price_incl_tax, sales_flat_order_grid.grand_total, sales_flat_order_grid.entity_id FROM sales_flat_order, sales_flat_order_grid, sales_flat_order_item WHERE sales_flat_order.entity_id=sales_flat_order_grid.entity_id AND sales_flat_order_grid.entity_id=order_id AND sales_flat_order_grid.created_at NOT LIKE '2012%'AND sales_flat_order_grid.created_at NOT LIKE '2013%' GROUP BY entity_id";
$result = mysqli_query($db, $query);
fputcsv($fp,$map);
while($row = mysqli_fetch_assoc($result))
{
fputcsv($fp, $row);
}
fclose($fp);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.