PHP到CSV - 将一行分成多行

时间:2014-05-30 06:13:33

标签: php mysql csv export-to-csv

我有一些PHP代码将数据库表转换为csv文件 - 这是一种享受。

数据库信息由Modx(CMS)登录/用户注册系统输入。

这被添加到一行,这很好但我现在需要在csv中彼此之间的一些列。所以当我需要整理某些东西时,我可以订购和组织csv。例如,按州状态对所有用户进行排序。

以下是一个例子:

ID | memeberNo | branch name 1 | branch state 1 | branch country 1 | branch name 2 | branch state 2 | branch country 2 etc.... up to 15 branches.

我需要在每个分支之后换行,所以它就像这样:

ID | memeberNo | branch name 1 | branch state 1 | branch country 1 (line break here???)
                 branch name 2 | branch state 2 | branch country

这有意义吗?我宁愿让php做这个而不是数据库因为我不想弄乱登录系统。 所以我猜它需要在第21列之后换行,然后每6列,同时添加20列,以便它们排成一行。

$entire = $_POST['entire'];
if ($entire == "yes"){
global $modx;
$delete =$_POST['database'];
$modx->db->delete($delete);
}
if ($entire == "no"){
global $modx;
$delete =$_POST['database'];
$id =$_POST['id'];
$modx->db->delete($delete,"id=$id");
}
if ($entire == "export"){
global $modx;
$delete =$_POST['database'];

$result = $modx->db->select("*", $delete );

if (!$result) die('Couldn\'t fetch records');
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++) {
     $headers[] = mysql_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

     fputcsv($fp, array_values($row));

}
die;
}
}

1 个答案:

答案 0 :(得分:0)

<?php
$entire = $_POST['entire'];
if ($entire == "yes"){
global $modx;
$delete =$_POST['database'];
$modx->db->delete($delete);
}
if ($entire == "no"){
global $modx;
$delete =$_POST['database'];
$id =$_POST['id'];
$modx->db->delete($delete,"id=$id");
}
if ($entire == "export"){
global $modx;
$delete =$_POST['database'];

//$result = $modx->db->select("*", $delete );



$output = "";
$sql = $modx->db->select("*", $delete );
$columns_total = mysql_num_fields($sql);

// Get The Field Name

for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="\n";

// Get Records from the table

while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
if ($i == 25){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 32){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 39){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 46){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 53){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 60){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 67){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 74){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 81){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 88){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 95){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 102){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 109){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}elseif ($i == 116){
    $output .="\n";
    $output .=",,,,,,,,,,,,,,,,,,";
    $output .='"'.$row["$i"].'",';
}else {
    $output .='"'.$row["$i"].'",';  
}
}
$output .="\n";
}

// Download the file

$filename = "myFile.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);

echo $output;
exit;

}
?>