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;
    }
}
  • 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 . ',';}. – maxb Apr 12 at 7:35
  • Typo in csv writing foreach. Secong foreach should be foreach($order as $csvParcel) – Binary Impact BIG Apr 12 at 8:36

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.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.