在特定id之后插入第二列到csv中

问题描述 投票:1回答:2

我有一个名为A的表,有1000行像:

id  |  name
----+------
1   |  a
2   |  b
3   |  c
4   |  d

等到id=1000

现在我想把这个表A导出为CSV,我能够做到。但我希望数据在2列中,如数据直到id=499应在第1列打印,剩余数据应在第2列打印。我不是在id打印CSV

CSV中结果应该是这样的:

a   |  at500   (I am using 500 for reference that from here it should print in 2nd column)
b   |  bvf501
c499|  vf1000

这是我到现在为止将所有行打印成单列的代码:

$filename = "excelfilename";
$sep = "\t";

$sql = "Select `name` from `A`";
$result = mysqli_query($con,$sql) or die("Couldn't execute query:<br>" . 
mysqli_error($con). "<br>" . mysql_errno());    
$file_ending = "xls";
//header info for browser
header("Content-Type: application/xls");    
header("Content-Disposition: attachment; filename=$filename.xls");  
header("Pragma: no-cache"); 
header("Expires: 0");

 while($row = mysqli_fetch_row($result))
{
    $schema_insert = "";
    for($j=0; $j<mysqli_num_fields($result);$j++)
    {
        if(!isset($row[$j]))
            $schema_insert .= "NULL".$sep;
        elseif ($row[$j] != "")
            $schema_insert .= "$row[$j]".$sep;

        else
            $schema_insert .= "".$sep;

    }

    $schema_insert = str_replace($sep."$", "", $schema_insert);
    $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
    $schema_insert .= "\t";
    print(trim($schema_insert));
    print "\n";
}   
php mysql csv export-to-csv mysql-5.7
2个回答
0
投票

首先,您可以从DB读取数据并将结果保存在数组中。

考虑一下,$ full_data_array是你的1000'名字'

然后你可以将数组拆分为两个(因为你需要500个),像这样,

$chunk_array = array_chunk($full_data_array, 2);

您将获得多维数字索引数组。

像这样组合这两个数组,

$combined_array = array_map(function ($a1, $a2) { return $a1 . ' , ' . $a2; } , $chunk_array[0], $chunk_array[1]);

然后遍历这个$ combined_array来创建这样的CSV,

$file = fopen("your_csv_name.csv","w");
foreach ($combined_array as $line)
  {
  fputcsv($file,explode(',',$line));
  }
fclose($file);

0
投票
$filename = "excelfilename";
$sep = "\t";

$sql = "Select `name` from `A`";
$result = mysqli_query($con,$sql) or die("Couldn't execute query:<br>" . 
mysqli_error($con). "<br>" . mysql_errno());    
$file_ending = "xls";
//header info for browser
header("Content-Type: application/xls");    
header("Content-Disposition: attachment; filename=$filename.xls");  
header("Pragma: no-cache"); 
header("Expires: 0");

$tempData="";
 while($row = mysqli_fetch_row($result))
{
    $tempData=$row;
}   

$size=count($tempData)/2;
for($i=0;$i<$size;$i++){
    $schema_insert1 = "";
    for($j=0; $j<count($tempData[$i]);$j++)
    {
        if(!isset($tempData[$i][$j]))
            $schema_insert1 .= "NULL".$sep;
        elseif ($tempData[$i][$j] != "")
            $schema_insert1 .= $tempData[$i][$j].$sep;

        else
            $schema_insert1 .= "".$sep;

    }

    $schema_insert2 = "";
    for($j=0; $j<count($tempData[$i+$size]);$j++)
    {
        if(!isset($tempData[$i+$size][$j]))
            $schema_insert2 .= "NULL".$sep;
        elseif ($tempData[$i+$size][$j] != "")
            $schema_insert2 .= $tempData[$i+$size][$j].$sep;

        else
            $schema_insert2 .= "".$sep;

    }

    $schema_insert1 = str_replace($sep."$", "", $schema_insert1);
    $schema_insert2 = str_replace($sep."$", "", $schema_insert2);

    // Now concatenation them both strings as you want
    // $tempString .= $schema_insert1.$sep.$schema_insert2;
}

根据您的要求更换或更改// $tempString .= $schema_insert1.$sep.$schema_insert2;此行。

希望这对你有用。

© www.soinside.com 2019 - 2024. All rights reserved.