在CSV中导出列标题

时间:2019-03-13 08:49:47

标签: php mysql csv mysqli export

我必须改进此代码,以便在CSV文件上获得更好的输出。 我需要的是也导出列标题,并可能用自定义值替换本机列标题。 同样在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);
    ?>

您有什么建议吗? 非常感谢!

我刚刚编辑了这段代码

 {
  while ($row = $rs->fetch_assoc()) 
  {
    fputcsv($fp, array_values($map));
    fputcsv($fp, array_values($row));
  }

但是现在,在CSV文件中,我有一个叫做$ map的数组。 当然,我只需要一次 谢谢

1 个答案:

答案 0 :(得分:0)

我解决了这样更改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);  

?>