格式化 php 创建的 CSV 文件(标题在顶行,数据在第二行)

发布于 2024-10-21 05:15:30 字数 1350 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2024-10-28 05:15:30

你可以改变你的代码:

<?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, Age, Gender, Country, Favorite colour, Favorite search engine
<?php echo $row['first_name']." ".$row['last_name']; ?>, <?php echo $row['age']; ?>, <?php echo $row['gender'] == 'm' ? 'Male' : 'Female'; ?>, <?php echo getCountry($row['country']); ?>, <?php echo getColour($row['colour']); ?>, <?php echo getSearchEngines($row['search_engine']); ?>

<?php

}
?>

you can change you code up:

<?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, Age, Gender, Country, Favorite colour, Favorite search engine
<?php echo $row['first_name']." ".$row['last_name']; ?>, <?php echo $row['age']; ?>, <?php echo $row['gender'] == 'm' ? 'Male' : 'Female'; ?>, <?php echo getCountry($row['country']); ?>, <?php echo getColour($row['colour']); ?>, <?php echo getSearchEngines($row['search_engine']); ?>

<?php

}
?>
药祭#氼 2024-10-28 05:15:30

我认为你的数据如下 -

$records = array(
               [0] = array(heading=>data 1, heading 2=>data 2, heading 3=>data 3),
               [1] = array(heading=>data 1, heading 2=>data 2, heading 3=>data 3),
               [2] = array(heading=>data 1, heading 2=>data 2, heading 3=>data 3),
               [3] = array(heading=>data 1, heading 2=>data 2, heading 3=>data 3)
           );

你可以遵循以下概念 -

$heading = implode(',', array_keys( array_pop($records) ) );

header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"yourfile.csv\"");

echo $heading."\n\r"; // HEADER IS BEING PLACED HERE

foreach($records as $row){ // CONTENTS IS BEING PLACED HERE
    echo implode(',', $row)."\n\r";
}
exit;

I think your data are like bellow -

$records = array(
               [0] = array(heading=>data 1, heading 2=>data 2, heading 3=>data 3),
               [1] = array(heading=>data 1, heading 2=>data 2, heading 3=>data 3),
               [2] = array(heading=>data 1, heading 2=>data 2, heading 3=>data 3),
               [3] = array(heading=>data 1, heading 2=>data 2, heading 3=>data 3)
           );

You can follow the following concepts -

$heading = implode(',', array_keys( array_pop($records) ) );

header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"yourfile.csv\"");

echo $heading."\n\r"; // HEADER IS BEING PLACED HERE

foreach($records as $row){ // CONTENTS IS BEING PLACED HERE
    echo implode(',', $row)."\n\r";
}
exit;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文