我已经写了一个PHP代码下载报告作为CSV文件格式。但我需要做的是,我需要对齐如果DIDNo有2个或更多的AgentNumber那么我需要把它放在旁边
ex:
DIDNo AgentNumber
001 12334554 12334578 1234556
<?php
require_once('common.php');
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// output the column headings
fputcsv($output, array('DIDNo','AgentNumber'));
// fetch the data
$sql = "SELECT didNo,agentNumber FROM AGENT_INFO";
$rows = mysql_query($sql);
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
?>
I am getting:
--------------------------------------------------
DIDNo AgentNumber
3054001 994740040
3054002 994734911
3054002 908799144
3054003 999497774
3054003 960007775
3054004 442853588
3054004 999470955
3054004 960044599
3054005 999458354
3054005 814461211
我需要像这样:
DIDNo AgentNumber
3054001 999440040
3054002 999434911 908799944
3054002
3054003 999497774 960007775
3054003
3054004 442853588 999470955 960044599
3054004
3054004
3054005 999458354 814461211
3054005
$sql = "SELECT didNo,
GROUP_CONCAT(agentNumber SEPARATOR ',') as agentNumbers
FROM AGENT_INFO
GROUP BY didNo";
$rows = mysql_query($sql);
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) {
$data = array();
foreach($row as $value) {
$data = array_merge($data, explode(',', $value));
}
fputcsv($output, $data);
}
(未测试)