2014-04-12 3 views
0

내 PHP 스크립트에서 생성 된 데이터 테이블에서 다운로드 가능한 CSV 파일을 원합니다. 스크립트를 실행하면 다운로드하라는 메시지가 표시되지만 CSV 파일에 모든 코드 (태그 등 포함)를 버린 것처럼 보입니다. 필요한 것은 표에 포함 된 결과뿐입니다. 필자는이 머리 위로 내 머리카락을 찢어 버렸다! 어떤 아이디어? 내 스크립트를 heres PHP 부분 :배열을 CSV 파일로 출력 변환

<?php 
session_start(); 
header('Content-Type: text/csv'); 
header('Content-Disposition: attachment; filename='. 'file.csv'); 
header('Pragma: no-cache'); 
header("Expires: 0"); 

$serverName = "localhost\SQLEXPRESS"; 
$connectionInfo = array("Database"=>"AuServer", "ReturnDatesAsStrings"=>"true"); 
$conn = sqlsrv_connect($serverName, $connectionInfo); 

$sql = "select top 100 AuEvent.time_stamp, AusDatabase.Title, AuEvent.user_name, AuEvent.rec_id,          LogEventDescription.description 
     FROM AuEvent 
     INNER JOIN LogEventDescription 
     ON AuEvent.event_id=LogEventDescription.event_id 
     INNER JOIN AusDatabase 
     ON AuEvent.db_id=AusDatabase.ID 
     WHERE LogEventDescription.lcid='1033' 
     ORDER BY time_stamp DESC"; 

    $stmt = sqlsrv_query($conn, $sql); 
    if($stmt === false) { 
     die(print_r(sqlsrv_errors(), true)); 
} 

//sort results into an array for the table 
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){ 
    echo " 
     <tr> 
     <td>". $row['time_stamp'] . "</td> 
     <td><a href=\"dbfilter.php?id=". $row['Title'] . "\">". $row['Title'] . "</a></td> 
     <td><a href=\"userfilter.php?id=". $row['user_name'] . "\">". $row['user_name'] . "</a></td> 
     <td><a href=\"eventfilter.php?id=". $row['description'] . "\">". $row['description'] . "</a></td> 
     <td>". $row['rec_id'] . "</td> 
     </tr>"; 



$fp = fopen('file.csv', 'w'); 

foreach ($row as $fields) { 
fputcsv($fp, $row); 
} 

} 
?> 
+0

출력물을 다운로드 할 수 있도록 하시겠습니까? 아니면 다운로드 할 수있는 파일을 만들고 싶습니까? – iamsleepy

+0

데이터를 다운로드 할 수있는 CSV 파일에 넣으려고합니다. – user3361017

+0

fputcsv를 while 루프에 넣고 csv 파일에 $ row [ '뭐든']를 쓰십시오. –

답변

0

당신이 다음 다운로드 파일을 생성 테이블 데이터

<?php 

session_start(); 
$serverName = "localhost\SQLEXPRESS"; 
$connectionInfo = array("Database" => "AuServer", "ReturnDatesAsStrings" => "true"); 
$conn = sqlsrv_connect($serverName, $connectionInfo); 

$sql = "select top 100 AuEvent.time_stamp, AusDatabase.Title, AuEvent.user_name, AuEvent.rec_id,          LogEventDescription.description 
     FROM AuEvent 
     INNER JOIN LogEventDescription 
     ON AuEvent.event_id=LogEventDescription.event_id 
     INNER JOIN AusDatabase 
     ON AuEvent.db_id=AusDatabase.ID 
     WHERE LogEventDescription.lcid='1033' 
     ORDER BY time_stamp DESC"; 

$stmt = sqlsrv_query($conn, $sql); 
if ($stmt === false) { 
    die(print_r(sqlsrv_errors(), true)); 
} 

$fp = fopen('file.csv', 'w'); 
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { 
    foreach ($row as $fields) { 
     fputcsv($fp, $row); 
    } 
} 

$path = path/to/file; 
header('Content-Type: text/csv'); 
header('Content-Disposition: attachment; filename="'.basename($file).'"'); 
header('Content-Length: '.filesize($file)); 
readfile($file); 

?> 

과 반향 중지하려면 당신은 인쇄 및 다운로드 파일 모두를 원하는 경우 다음

<?php 

session_start(); 
$serverName = "localhost\SQLEXPRESS"; 
$connectionInfo = array("Database" => "AuServer", "ReturnDatesAsStrings" => "true"); 
$conn = sqlsrv_connect($serverName, $connectionInfo); 

$sql = "select top 100 AuEvent.time_stamp, AusDatabase.Title, AuEvent.user_name, AuEvent.rec_id,          LogEventDescription.description 
     FROM AuEvent 
     INNER JOIN LogEventDescription 
     ON AuEvent.event_id=LogEventDescription.event_id 
     INNER JOIN AusDatabase 
     ON AuEvent.db_id=AusDatabase.ID 
     WHERE LogEventDescription.lcid='1033' 
     ORDER BY time_stamp DESC"; 

$stmt = sqlsrv_query($conn, $sql); 
if ($stmt === false) { 
    die(print_r(sqlsrv_errors(), true)); 
} 

$fp = fopen('file.csv', 'w'); 
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { 
    foreach ($row as $fields) { 
     fputcsv($fp, $row); 
    } 
} 

$path = path/to/file; 
header('Content-Type: text/csv'); 
header('Content-Disposition: attachment; filename="'.basename($file).'"'); 
header('Content-Length: '.filesize($file)); 
readfile($file); 

ob_end_clean(); 
ob_start(); 

while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { 
    echo "echo all data here, tr and td etc"; 
} 
ob_flush(); 
?>