格式化 php 创建的 CSV 文件(标题在顶行,数据在第二行)
header("内容类型:应用程序/八位字节流"); header("Content-Disposition: Attachment; filename=\"yourfile.csv\"");
我正在使用它来生成 CSV 文件;
目前,它将数据显示为:
标题、数据、标题 2、数据 2、标题 3、数据 3...
我想将数据显示为;
标题,标题 2,标题 3
数据 1,数据 2,数据 3
这将如何完成?
<?php
$sql = "SELECT * FROM survey
WHERE id = '".sql_escape($s)."'";
$row = fetch0ne($sql);
if(!empty($row)) {
$int = array();
$sql = "SELECT * FROM survey_interests
WHERE survey = '".sql_escape($s)."'";
$interests = fetchAll($sql);
if(!empty($interests)) {
foreach($interests as $item) {
$int[] = getInterest($item['interest']);
}
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"yourfile.csv\"");
?>
Full Name, <?php echo $row['first_name']." ".$row['last_name']; ?>, Age, <?php echo $row['age']; ?>, Gender, <?php echo $row['gender'] == 'm' ? 'Male' : 'Female'; ?>, Country, <?php echo getCountry($row['country']); ?>, <?php if(!empty($int)) { ?><?php echo implode(", ",$int); ?><?php } ?>, Favorite colour, <?php echo getColour($row['colour']); ?>, Favorite search engine, <?php echo getSearchEngines($row['search_engine']); ?>
<?php
}
?>
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"yourfile.csv\"");
i am using this to produce a CSV file;
Currently it is displaying the data as;
Heading, Data, Heading 2, Data 2, Heading 3, Data 3...
i want to display the data as;
heading, heading 2, heading 3
data 1, data 2, data 3
How would this be done ?
<?php
$sql = "SELECT * FROM survey
WHERE id = '".sql_escape($s)."'";
$row = fetch0ne($sql);
if(!empty($row)) {
$int = array();
$sql = "SELECT * FROM survey_interests
WHERE survey = '".sql_escape($s)."'";
$interests = fetchAll($sql);
if(!empty($interests)) {
foreach($interests as $item) {
$int[] = getInterest($item['interest']);
}
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"yourfile.csv\"");
?>
Full Name, <?php echo $row['first_name']." ".$row['last_name']; ?>, Age, <?php echo $row['age']; ?>, Gender, <?php echo $row['gender'] == 'm' ? 'Male' : 'Female'; ?>, Country, <?php echo getCountry($row['country']); ?>, <?php if(!empty($int)) { ?><?php echo implode(", ",$int); ?><?php } ?>, Favorite colour, <?php echo getColour($row['colour']); ?>, Favorite search engine, <?php echo getSearchEngines($row['search_engine']); ?>
<?php
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以改变你的代码:
you can change you code up:
我认为你的数据如下 -
你可以遵循以下概念 -
I think your data are like bellow -
You can follow the following concepts -