2013-07-10 108 views
0

我有這個查詢在這裏,這是我輸出它的方式csv,有人可以幫助我嗎? 這裏很麻煩,如果我將它打印爲Excel,並且我嘗試了一些版本,但是我使用odbc查詢的事實使它對我適應它與我找到的示例有點困難線上。問題與打印查詢爲csv

$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; 
+0

除了這個事實,這將是一個容易得多,如果你使用PHP的內置[fputcsv()( http://www.php.net/manual/en/function.fputcsv.php)函數將每行寫入一個CSV文件行....您的問題到底是什麼? –

+0

問題是,當我打開CSV沒有什麼是它的地方,行被搞砸了,他們沒有分開.. – Alb

+1

你沒有在每行的末尾放回一個....使用fputcsv( ),你不必擔心這一點,或者從你的數據庫中檢索到的包含逗號的列,或者你沒有檢查的任何其他潛在的問題 –

回答

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); 
+0

感謝您的答案,但請我得到這個錯誤: \t \t \t \t \t 調用堆棧 – Alb

+0

對不起...它是strip_tags而不是striptags – Orangepill

+1

這樣更好'header(「Content-Disposition:attachment; filename ='Kondicioner」。date('dm -Y')。「.csv'」);'filename filename在報價 – mishka