0

我在这里有这个查询,这是我将它输出到 csv 的方式,有人可以帮助我吗?那里很乱,如果我将它打印为 Excel 会更好,并且我尝试了一些版本,但是我使用 odbc 查询的事实让我很难适应我在网上找到的示例.

$result = "SELECT forma.*, SMS_MONTIME.IDTICKET,SMS_MONTIME.MBYLLUR,
SMS_MONTIME.time_added FROM forma 
LEFT JOIN SMS_MONTIME ON forma.ID = SMS_MONTIME.IDTICKET WHERE 
forma.data_fillim >= '$df' AND forma.data_fillim <= '$dm' ORDER BY forma.id DESC"; 
$user_query = odbc_exec($connection, $result) or die(odbc_error());
//While loop to fetch the records
while($row = odbc_fetch_array($user_query))
{
$contents.=$row['klienti'].",";
$contents.=$row['id'].",";
$contents.=$row['tekniku_emer'].",";
$contents.=$row['telefoni'].",";
$contents.=$row['data_fillim'].",";
$contents.=$row['difekti'].",";
$contents.=$row['tekniku_mesazh'].",";
$contents.=$row['shenime'].",";

}

// remove html and php tags etc.
$contents = strip_tags($contents); 

//header to make force download the file
header("Content-Disposition: attachment; filename=Kondicioner".date('d-m-Y').".csv");
print $contents;
4

1 回答 1

1

输出 csv 的更好方法是使用 fputcsv 函数。

<?php
$result = "SELECT forma.*, SMS_MONTIME.IDTICKET,SMS_MONTIME.MBYLLUR,
SMS_MONTIME.time_added FROM forma 
LEFT JOIN SMS_MONTIME ON forma.ID = SMS_MONTIME.IDTICKET WHERE 
forma.data_fillim >= '$df' AND forma.data_fillim <= '$dm' ORDER BY forma.id DESC"; 
$user_query = odbc_exec($connection, $result) or die(odbc_error());
//While loop to fetch the records
header("Content-Disposition: attachment; filename='Kondicioner".date('d-m-Y').".csv'");
$fp = fopen("php://output","w");
// here you set up your header
fputcsv($fp, array("klienti","id","tekniku_emer","telefoni","data_fillim","difekti","tekniku_mesazh", "shenime");
while($row = odbc_fetch_array($user_query))
{
   $row = array_map("strip_tags", $row);
   fputcsv($fp, array($row['klienti'],$row['id'],
                      $row['tekniku_emer'],$row['telefoni'],
                      $row['data_fillim'],$row['difekti'],
                      $row['tekniku_mesazh'],$row['shenime']));

}
fclose($fp);
于 2013-07-10T07:34:45.367 回答