【问题标题】:PHP to CSV - break one row into multiple rowsPHP to CSV - 将一行分成多行
【发布时间】:2014-07-19 20:05:35
【问题描述】:

我有一些 php 代码可以将 DB 表转换为 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 做这件事,也不愿让 DB 做这件事,因为我不想弄乱登录系统。 所以我想它需要在第 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;
}
}

【问题讨论】:

  • 格式化一个 CSV 文件会在未来给你带来难以言喻的悲痛——它只是不适合你的想法。无论如何,您正在运行SELECT 查询来获取数据,那么将数据库以您需要的格式获取到SELECT 有什么问题?如果您需要操作数据,SELECT 将其放入不同的表中并在那里进行操作。
  • 我无法用我有限的 php 知识进行工作。
  • 有点想通了:

标签: php mysql csv export-to-csv


【解决方案1】:
<?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;

}
?>

【讨论】:

    猜你喜欢
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多