简体   繁体   中英

PHP csv file exported with results in columns and rows

What I'm trying to do is with this PHP script is read a specific code that has different results depending on the digit in its location.
So the code starts in position 709 in the txt file and looks like: FRNARUFY16UEAE5QC
that's all fine and good and I get my results that I want my problem arises in the exported CSV file.
I managed to get each result to be displayed on a new line with \r\n but I don't know how to get each result to be on a new column.
Ideally I wish to make the final result like this在此处输入图像描述

I have tried a few solutions I found but none have worked and I suspect it's the fault of my code rather than the solutions themselves so any help would be appreciated.

<?php

$files = glob('C:/Users/X/Desktop/php/in/file*.{txt}', GLOB_BRACE);

$vinsToUpdate = array();

if ( $files !== false )
{
    $filecount = count( $files );

    foreach($files as $file)
    {
            $handle = fopen($file, "r");
            if ($handle)
            {
                while (($line = fgets($handle)) !== false)
                {
                  if(strlen($line) > 1)
                  {
                      $csv = str_getcsv($line);
                      if(substr($line,708,1) === 'F' && substr($line,709,2) === 'RN' && substr($line,715,1) === 'Y')
                      {
                            echo $vinsToUpdate ="Vin ".substr($line,10,17), " F16 1 Petrol 8703211000 1211 1211 uk \r\n";

                      }
                      if(substr($line,708,1) === 'F' && substr($line,709,2) === 'RN' && substr($line,715,1) === 'L')
                                {
                                  echo $vinsToUpdate ="Vin ".substr($line,10,17), " F16 1 Petrol 8703211000 1159 1159 uk \r\n";
                                }
                    if(substr($line,708,1) === 'F' && substr($line,709,2) === 'SD' && substr($line,715,1) === '9' && substr($line,716,4) === 'ZE16')
                                          {
                                            echo $vinsToUpdate ="Vin ".substr($line,10,17), " ZE1 40kwh Electric 8703801000 1515 1515 UK \r\n";
                                          }
                        if(substr($line,708,1) === 'F' && substr($line,709,2) === 'SD' && substr($line,715,1) === '9' && substr($line,716,4) === 'ZE18')
                                            {
                                                echo $vinsToUpdate ="Vin ".substr($line,10,17), " ZE1 60kwh Electric 8703801000 1708 1708 UK \r\n";
                                            }
                        if(substr($line,708,1) === 'T' && substr($line,709,2) === 'DZ' && substr($line,715,1) === 'Y')
                                            {
                                                echo $vinsToUpdate ="Vin ".substr($line,10,17), " J12 1.3   Petrol 8703221000   1466    1466    UK \r\n";
                                            }
                        if(substr($line,708,1) === 'T' && substr($line,709,2) === 'DZ' && substr($line,715,1) === 'W')
                                            {
                                                echo $vinsToUpdate ="Vin ".substr($line,10,17), " J12 1.3   Petrol 8703221000 1511 1511 UK \r\n";
                                            }
                        if(substr($line,708,1) === 'T' && substr($line,709,2) === 'DZ' && substr($line,715,1) === 'W')
                                            {
                                                echo $vinsToUpdate ="Vin ".substr($line,10,17), " J12 1.3   Petrol 8703221000 1570 1570 UK \r\n";
                                            }
                  }
                }
                    fclose($handle);
            } else {
                // error opening the file.
            }
    }
}

// Excel file name for download
$fileName = "Spec" . ".csv";

// Headers for download
header("Content-Disposition: attachment; filename=\"$fileName\"");
header("Content-Type: application/vnd.ms-excel");


exit;




?>

You are nearly there,

CSV files are super basic, and it actually stands for Comma, Separated, Value.

So to go to the next column, you simply add a comma to separate into columns.

echo $vinsToUpdate ="Vin ".substr($line,10,17), " F16, 1, Petrol, 8703211000, 1211, 1211, uk";

etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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