繁体   English   中英

将特定ID插入到csv后插入第二列

[英]Insert into second column after specific id into csv

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

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

依此类推,直到id=1000

现在,我想将此表A导出为CSV 但是我想要2列中的数据,例如直到id=499数据应在第一列中打印,其余数据应在第二列中打印。 而且我没有在CSV打印id

结果应该像这样在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";
}   

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

考虑一下,$ 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);
$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; 这符合您的要求。

希望这项工作对您有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM