1
\$\begingroup\$

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;
 }
}
asked Apr 12, 2018 at 7:00
\$\endgroup\$
2
  • \$\begingroup\$ How come you're fetching all attributes through function calls? why not do e.g. $orders->orderNumber? With a structure like that, you could just do foreach ($orders as $key => $value) { ... } and loop through all attributes. Otherwise you could do something like foreach ($my_keys as $key) {$output_string .= $orders->$key . ',';}. \$\endgroup\$ Commented Apr 12, 2018 at 7:35
  • \$\begingroup\$ Typo in csv writing foreach. Secong foreach should be foreach($order as $csvParcel) \$\endgroup\$ Commented Apr 12, 2018 at 8:36

1 Answer 1

2
\$\begingroup\$

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
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.