这是我第一次尝试将数据导出到csv中,任何人都可以告诉我如何在下面执行此操作,我的代码是我自己尝试但是它不起作用,
它只是在cvs文件中打印标题而不是在cvs中打印数据,任何人都可以指导我在哪里错了.......
Thnaks
Filterdata.php
<?php
$mediaexport=$_GET["media"];
?>
<form action = "cmsexp_to_excel.php" method = "post">
<input checked type='checkbox' name="media" value="<?php echo $mediaexport;?>" style='visibility:hidden' >
<input type="image" value="" src="/img/export.png" class="export" style='margin:0 5px -9px 183px;cursor:pointer;'>
</form>
cmsexp_to_excel.php
<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxxxxxxx';
$dbPassword = 'xxxxxxxxx';
$dbDatabase = 'xxxxxxxxxx';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) ;
mysql_select_db ($dbDatabase, $db) ;
$mediaexport=$_GET["media"];
$values = "SELECT DISTINCT contact.`id` , contact.`contactgroup` , contact.`media` , contact.`media2` , contact.`email1` , contact.`nationality` , contact.`country3` , contact.`twon` , contact.`area` , contact.`gender` , contact.`married` , contact.`children` , contact.`driverslicense`
FROM contact";
$header = "Media" . "\t";
$header .= "E-mail" . "\t";
$header .= "Nationality" . "\t";
$header .= "City" . "\t";
while( $row1 = mysql_fetch_assoc($values)){
$row1[] = $row['media'];
$row1[] = $row['email1'];
$row1[] = $row['ntionality'];
$row1[] = $row['city'];
$data .= join(',', $row1)."\n";
}
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=Inbox-Reprot.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
exit();
?>
答案 0 :(得分:0)
所以,你有一个多维数组,如下所示:
$my_array = array(
array("First Name", "Last Name", "Address", "City"),
array("Marty", "McFly", "9303 Lyon Drive", "Hill Valley"),
array("Emmett", "Brown", "1640 Riverside Drive", "Hill Valley")
);
因此,您应该将标头设置为application/csv
,并将echo
数组中的所有值设置为逗号分隔:
header("Content-type: application/csv");
header("Content-disposition: attachment; filename=your_file_name.csv");
foreach ($my_array as $row) {
echo implode(",", $row) . "\r\n";
}
PHPFiddle链接:http://phpfiddle.org/main/code/v2w6-cxv9