简体   繁体   中英

Export column title in csv

I have to improve this code for a better output on my CSV file. 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

<?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

I Solved changing the php like this:

<?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);  

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM