简体   繁体   中英

Multidimensional Array to CSV in Laravel

I have a Laravel application that runs a command to assigns techs to a job listed in a csv. The csv is parsed into an array and the array is filtered before elements are added based on business requirements. I am having issues in generating the csv as when i hit the URI the error I receive is "Site cannot be reached. Err Invalid Response".

Here is a sample of my array which I am getting from the results of a function.

    array (
)array (
  21 =>
  array (
    'JobNumber' => '2',
    'JobType' => '3',
    'Node' => '10',
    'fname' => 'RICARDO',
    'lname' => 'SMITH',
    'RAddress' => 'SUGARAPPLE ST',
    'HomePhone' => '3924651',
    'WorkPhone' => '3276200',
    'RTime' => '10-12',
    'Comment' => 'FROM POLICE STATION, EAST INTO PINEWOOD..3RD LFT ONTO SUGARAPPLE ST;  5TH HSE ON RGT; BEIGE/YELLW #19',
    'FTax' => '1.00',
    'Tag' => '010106',
    'QuotaGroup' => '1.00',
    'Cust_Acct' => '10221401',
    'offernum' => '2136370',
    'installer' => 'Aaron Cash',
  ),
  122 =>
  array (
    'JobNumber' => '30',
    'JobType' => '3',
    'Node' => '213',
    'fname' => 'MONIQUE',
    'lname' => 'SAWYER NAIRN',
    'RAddress' => 'SUTTON ST',
    'HomePhone' => '8017750',
    'WorkPhone' => '2250417',
    'RTime' => '10-12',
    'Comment' => 'TRN ONTO KEMP RD FRM SHIRLEY ST,   1ST LFT BY LODGE BLDG, 2ND RT,     UNPAINTED HSE AT DEAD END. #13     ...SDW',
    'FTax' => '1.00',
    'Tag' => '213308',
    'QuotaGroup' => '1.00',
    'Cust_Acct' => '11390602',
    'offernum' => '2137494',
    'installer' => 'Wayne Forbes',

Controller:

<?php

namespace App\Http\Controllers;
use App\Console\Commands\NPInstallersAssigner;
use Illuminate\Http\Request;

class GetCsvController extends Controller
{
    public function getCsv()
    {
        $npinstallerassigner = new NPInstallersAssigner();
        $results = $npinstallerassigner->assignEasternJobs();
            $filename = 'userData.csv';
        header("Content-type: text/csv");
        header("Content-Disposition: attachment; filename=$filename");
        $output = fopen("php://output", "w");
        $header = array_keys($results[0]);
        fputcsv($output, $header);
        foreach ($results as $row) {
            fputcsv($output, $row);
        }
        fclose($output);
    }
}

I believe that the code being used to execute the array to csv is formatted off maybe?

maybe try and add a cache-control header and encapsulate the filename in the header with quotes and don't forget to exit the script after your done outputting.

header('Content-Type: text/csv; charset=utf-8', true);
$file_name = 'userData.cvs';
header('Cache-Control: no-cache, must-revalidate');            
header('Content-Disposition: attachment; filename="'.$file_name.'.csv"', true);
$output = fopen('php://output', 'w');
  /// write to cvs
exit();

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