2015-05-29 81 views
-1

我有一個問題,試圖通過PHP和Silex創建可下載的CSV。Silex和fputcsv問題

這是我到現在爲止

header('Content-Type: text/csv; charset=utf-8'); 
header('Content-Disposition: attachment; filename=csv.csv'); 


$output = fopen('php://output', 'w'); 
fputcsv($output, array('Referrer', 'Occurrence')); 

$list = array(); 

$html = "<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.min.js'></script><script type='text/javascript' src='sort/jquery.tablesorter.min.js'></script>" 
     . "</script><script type='text/javascript' src='sort/jquery.tablesorter.js'></script>" 
     . "<script>" . '$' . "(document).ready(function(){" . '$' . "('#myTable').tablesorter();});" 
     . "" . '$' . "(document).ready(function(){" . '$' . "('#myTable').tablesorter({widthFixed: true, widgets: ['zebra']}).tablesorterPager({container: " . '$' . "('#pager')}); }); </script>" 
     . "<style>table{margin: 0 auto; border-collapse: collapse;}tr{border: solid black 2px;background-color: #E6E6E6;}" 
     . "td{border:solid black 2px;}tr:nth-child(odd){background-color: white;}" 
     . "th{width:200px; height:50px; border: solid black 2px;background-color: #B2CCFF;}" 
     . "#pager{margin : 0 auto ; text-align: center; padding: 20px;}</style>" 
     . "<table id='myTable' class='tablesorter' style='text-align:center;'><thead><tr><th>Referrer</th><th>" . $range . "</th>" . $conv . "</tr></thead><tbody>"; 
$sql = "SELECT * FROM " . $choice . " WHERE referrer like '%" . $data['Referrer'] . "%' AND " . $range . " >= " . $data['Data_di_inizio'] . " AND " . $range . " <= " . $data['Data_di_fine'] . ""; 
$stmt = $conn->query($sql); 

while ($row = $stmt->fetch()) { 
    if ($data['Tipologia'] == 1) { 
     $conv = "<td>" . $row['product_id'] . "</td>"; 
    } 
    $html .= "<tr><td>" . $row['referrer'] . "</td><td>" . $row[$range] . "</td>" . $conv . "</tr>"; 
    array_push($list, array_values($row)); 
} 

$html .= "</tbody></table><div id='pager' class='pager'><form> 
    <i class='prev'>&lArr;</i> 
<input type='text' class='pagedisplay'/> 
<i class='next'>&rArr;</i> 
<select class='pagesize'> 
    <option value='10'>10 per page</option> 
<option value='20'>20 per page</option> 
<option value='30'>30 per page</option> 
</select> 
</form> 
    </div>"; 

foreach ($list as $ferow) { 
    fputcsv($output, $ferow); 
} 
fclose($output); 


return $html; 
} 

在「當」的循環,在$行顯示沒有任何問題,和一張桌子,然後用所有正確的值被印刷它應該有。

當我得到csv下載,它也看起來很好。 表中的所有內容也都存在。但!最後,csv還包含以下內容:

<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.min.js'></script><script type='text/javascript' src='sort/jquery.tablesorter.min.js'></script></script><script type='text/javascript' src='sort/jquery.tablesorter.js'></script><script>$(document).ready(function(){$('#myTable').tablesorter() }) $(document).ready(function(){$('#myTable').tablesorter({widthFixed: true  widgets: ['zebra']}).tablesorterPager({container: $('#pager')})  }) </script><style>table{margin: 0 auto border-collapse: collapse }tr{border: solid black 2px background-color: #E6E6E6 }td{border:solid black 2px }tr:nth-child(odd){background-color: white }th{width:200px height:50px  border: solid black 2px background-color: #B2CCFF }#pager{margin : 0 auto  text-align: center padding: 20px }</style><table id='myTable' class='tablesorter' style='text-align:center 
     <i class='prev'>&lArr </i>                  
    <input type='text' class='pagedisplay'/>                  
    <i class='next'>&rArr </i>                 
    <select class='pagesize'>                 
     <option value='10'>10 per page</option>                  
    <option value='20'>20 per page</option>                 
    <option value='30'>30 per page</option>                 
    </select>                 
    </form>                 

它基本上也在CSV內打印所有在$ html變量中的內容。

我認爲這可能與輸出緩衝區有關,但我沒有想法。任何人有任何?

+0

你寫的CSV輸出增加exit();,然後返回'$ html'解決 - 和硅石(理所當然)寫道,在輸出了。 – Maerlyn

回答

0

問題得到通過後fclose($output);