简体   繁体   English

在CSV导出中输出列标题

[英]Outputting Column titles in CSV Export

I have this query that exports to a csv file. 我有这个导出到csv文件的查询。 It works fine the only thing i can't figure out is i need to export the column titles as well, and have them display as Full Name, UserName, Flag and Reason. 它工作得很好我唯一想不通的是我需要导出列标题,并让它们显示为全名,用户名,标志和原因。 Below is the code and it exports all the rows fine but I'm not sure how to export the column titles above the respected rows. 下面是代码,它导出所有行很好,但我不知道如何导出列标题上面的列标题。

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=blackflag_bidders.csv");
header("Pragma: no-cache");
header("Expires: 0");




//SQL Query for Data
$sql = "SELECT ui.first_name, ui.last_name, u.username,
    if(u.flag=1,'BLK', if(u.flag=2,'NAA','')) flag,
    if(u.flag!=0, IFNULL(ui.note,''),'') reason
FROM user u
LEFT JOIN user_info ui ON ui.user_id=u.id
WHERE u.flag!=0;";

//Prepare Query, Bind Parameters, Excute Query
$STH = $sam_db->prepare($sql);
$STH->execute();



//Export to .CSV
$fp = fopen('php://output', 'w');
//fputcsv($fp);
while ($row = $STH->fetch(PDO::FETCH_NUM)) fputcsv($fp,$row);
fclose($fp);

One way would be to fetch the first result by associative, those associative indices are columns anyway. 一种方法是通过关联获取第一个结果,那些关联索引无论如何都是列。 Apply array_keys to get those, then first add the headers, then the first fetched row, then loop the rest. 应用array_keys来获取它们,然后首先添加标题,然后是第一个获取的行,然后循环其余的。

// first set
$first_row = $STH->fetch(PDO::FETCH_ASSOC);
$headers = array_keys($first_row);
// $headers = array_map('ucfirst', $headers); // optional, capitalize first letter of headers
fputcsv($fp, $headers); // put the headers
fputcsv($fp, array_values($first_row)); // put the first row

while ($row = $STH->fetch(PDO::FETCH_NUM))  {
    fputcsv($fp,$row); // push the rest
}
fclose($fp);

You can get a column in CSV by simply displaying your results in Tabular form here in the page using <table> tag of HTML. 您只需使用HTML的<table>标签在页面中以表格形式显示结果,即可获得CSV列。

$result = "<table>";
    while ($row = $STH->fetch(PDO::FETCH_NUM)){
    $result .= "<tr><td>$row1</td><td>$row2</td><td>$row3</td></tr>";
}
$result .= "</table>";
fputcsv($fp, $result);

By $row1, $row2 , I mean the values you get in your resultset $row1, $row2 ,我的意思是你在结果集中得到的值

The answer to this will depend upon whether you already know the column names or not. 答案取决于您是否已经知道列名。 It seems like you do (eg you are already calling 'Select ui.firstname...') 好像你这样做(例如你已经在调用'Select ui.firstname ......')

If you do not, you can get the names by looking at this thread: What is the Select statement to return the column names in a table 如果不这样做,可以通过查看此线程来获取名称: 什么是Select语句以返回表中的列名

Once you have the names, you simply need to create a single row with the names and add them to file by modifying your code as: 获得名称后,只需创建一个包含名称的行,并通过修改代码将其添加到文件中:

//Export to .CSV
$columnNamesRow = "FirstName, LastName, UserName";
$fp = fopen('php://output', 'w');
fputcsv($fp, $columnNamesRow);

//fputcsv($fp);
while ($row = $STH->fetch(PDO::FETCH_NUM)) fputcsv($fp,$row);
fclose($fp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM