gpt4 book ai didi

PHP 到 CSV - 将一行分成多行

转载 作者:行者123 更新时间:2023-11-30 23:01:03 25 4
gpt4 key购买 nike

我有一些 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;
}
}

最佳答案

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

}
?>

关于PHP 到 CSV - 将一行分成多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23948221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com