\$\begingroup\$
\$\endgroup\$
2
I can tell by looking at this code that I've gone about this in the wrong way. Provided the string is correctly formatted (each index ofCSVData[][]
, would the following generate a CSV file?
There must be a more elegant way of taking my queries and building a CSV. Looking at this code kinda makes me sick in the stomach.
class ShippingCSVGenerator
{
public function generateShippingCSV($invoiceArray)
{
$invoiceArray = $this->filterInvoices($invoiceArray);
if(empty($invoiceArray))
{
// no parcels to fetch
return;
}
$dispatchDataRes = \DispatchDataQuery::create()
->where('DispatchData.printed = ?', $invoiceArray)
->find();
$index = 0;
$CSVData = [];
foreach($dispatchDataRes as $orders)
{
$parcelsRes = \ItemsInDispatchQuery::create()
->where('ItemsInDispatch.orderNumber = ?', $orders->orderNumber)
->find();
$parcelIndex = 0;
foreach($parclesRes as $parcel)
{
$CSVData[$index][$parcelIndex] =
$orders->orderNumber() . ',' .
$orders->date() . ',' .
$orders->toName() . ',' .
$orders->destinationBuilding() . ',' .
$orders->destinationStreet() . ',' .
$orders->suburb() . ',' .
$orders->unused_7() . ',' .
$orders->postcode() . ',' .
$orders->state() . ',' .
$orders->country() . ',' .
$orders->email() . ',' .
$orders->phone() . ',' .
$orders->itemName() . ',' .
$orders->itemPrice() . ',' .
$orders->instructions() . ',' .
$parcel->weight() . ',' .
$orders->shippingMethod() . ',' .
$parcelIndex . ',' .
$orders->SKU() . ',' .
$orders->quantity() . ',' .
$orders->unused_21() . ',' .
$orders->unused_22() . ',' .
$orders->unused_23() . ',' .
$orders->unused_24() . ',' .
$parcel->height() . ',' .
$parcel->width() . ',' .
$parcel->length() . ',' .
$orders->courier() . ',' .
$orders->unused_29() . ',' .
$orders->unused_30();
$parcelIndex++;
}
$index++;
}
header("Content-Type: text/csv");
header('Content-disposition: attachment;filename=shipping.csv');
$fp = fopen("php://output", "w");
fputcsv ($fp, $header, "\t");
foreach($CSVData as $order)
{
foreach($csvOrder as $csvParcel)
{
fputcsv($fp, $csvParcel, "\n");
}
}
fclose($fp);
}
private function filterInvoices($invoiceArray)
{
if(empty($invoiceArray))
{
// no parcels to fetch
return;
}
$filteredInvoices = [];
$dispatchDataRes = \DispatchDataQuery::create()
->where('DispatchData.printed = ?', $invoiceArray)
->find();
if($dispatchDataRes->isEmpty())
{
// no parcels to fetch
return;
}
else
{
foreach($dispatchDataRes as $invoicesToFetch)
{
$filteredInvoices = $invoicesToFetch->getInvoiceNumber();
}
}
return $filteredInvoices;
}
}
1 Answer 1
\$\begingroup\$
\$\endgroup\$
Encapsulate the csv line generation in another function.
function GenerateCSVlineFromArray($array){
$line = "";
foreach ($array as $value){
$line .= $value .",";
}
//cut last comma
$line = substr($line, 0, strlen($line) -1);
return $line;
}
Assuming you have already the right order of values.
answered Apr 12, 2018 at 8:39
lang-php
$orders->orderNumber
? With a structure like that, you could just doforeach ($orders as $key => $value) { ... }
and loop through all attributes. Otherwise you could do something likeforeach ($my_keys as $key) {$output_string .= $orders->$key . ',';}
. \$\endgroup\$foreach($order as $csvParcel)
\$\endgroup\$