开发者

Formatting a php created CSV file (headings on top row, data on second row)

header("Content-type: application/octet-stream"); header("Content-Di开发者_如何学Pythonsposition: 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

}
?>


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

}
?>


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜