多表mysql / php查询,带有两个输出到一个数组

时间:2013-09-25 13:20:29

标签: php mysql xml opencart

这需要花一点时间来解释,抱歉。

这段代码是opencart的xml vqmod的一部分,它将所选订单中的订单数据导出到Excel电子表格。在大多数情况下它工作正常。但是有一个障碍,如果有一个记录为特定订单列出了多个advanced_coupon,它将会废弃。

我确定我需要为该输出添加一个数组,并且它是折扣金额的相关字段,但我不知道该怎么做。我是php / MySQL的新手,但学得很快。如果我能指出正确的方向,那就太棒了。

我是新手,但在搜索中找不到相似内容。再次感谢。

以下是相关的代码:

    // The actual options data
    $i += 1;
    $j = 0;
      $query  = "SELECT o.*, op.name, op.model, op.order_product_id, op.quantity, op.price, op.total AS ptotal, op.product_id, op.tax, (SELECT amount FROM " . DB_PREFIX . "sales_promotion_history WHERE order_id = o.order_id) AS sales_promo_amount, (SELECT title FROM " . DB_PREFIX . "order_total ot WHERE ot.order_id = o.order_id AND ot.code = 'sales_promotion') AS sales_promo_code, (SELECT value FROM order_total ott WHERE ott.order_id = o.order_id AND ott.code = 'shipping') AS shipping_cost, (SELECT name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id) AS `order_status`, (SELECT ot.value FROM " . DB_PREFIX . "order_total ot WHERE ot.order_id = o.order_id AND ot.code = 'sub_total') AS sub_total, (SELECT ot.value FROM " . DB_PREFIX . "order_total ot WHERE ot.order_id = o.order_id AND ot.code = 'credit') AS store_credit, (SELECT ot.title FROM " . DB_PREFIX . "order_total ot WHERE ot.order_id = o.order_id AND ot.code = 'voucher') AS gift_code, (SELECT ot.text FROM " . DB_PREFIX . "order_total ot WHERE ot.order_id = o.order_id AND ot.code = 'voucher') AS gift_value, (SELECT title FROM " . DB_PREFIX . "order_total ot WHERE ot.order_id = o.order_id AND ot.code = 'advanced_coupon') AS advanced_coupon_code, (SELECT amount FROM " . DB_PREFIX . "advanced_coupon_history WHERE order_id = o.order_id) AS ac_amount, (SELECT ot.title FROM " . DB_PREFIX . "order_total ot WHERE ot.order_id = o.order_id AND ot.code = 'coupon') AS coupon_code, (SELECT ot.text FROM " . DB_PREFIX . "order_total ot WHERE ot.order_id = o.order_id AND ot.code = 'coupon') AS coupon_value FROM `" . DB_PREFIX . "order` o inner join `" . DB_PREFIX . "order_product` op on o.order_id=op.order_id  WHERE o.order_id IN ( ".$orderid." ) order by o.order_id";

    $result = $database->query( $query );
    foreach ($result->rows as $row) {
        $worksheet->writeString( $i, $j++, $row['order_id'] );
        $worksheet->writeString( $i, $j++, $row['date_added'] );            
        $worksheet->writeString( $i, $j++, $row['firstname'] );
        $worksheet->writeString( $i, $j++, $row['lastname'] );
        $worksheet->writeString( $i, $j++, $row['payment_method'] );
        $worksheet->writeString( $i, $j++, $row['shipping_firstname'] );
        $worksheet->writeString( $i, $j++, $row['shipping_lastname'] );
        $worksheet->writeString( $i, $j++, $row['shipping_company'] );
        $worksheet->writeString( $i, $j++, $row['shipping_address_1'] );
        $worksheet->writeString( $i, $j++, $row['shipping_address_2'] );
        $worksheet->writeString( $i, $j++, $row['shipping_city'] );
        $worksheet->writeString( $i, $j++, $row['shipping_postcode'] );
        $worksheet->writeString( $i, $j++, $row['shipping_zone'] );
        $worksheet->writeString( $i, $j++, $row['shipping_method'] );
        $worksheet->writeString( $i, $j++, $row['shipping_cost'] );
        $worksheet->writeString( $i, $j++, $row['total'] );
        $worksheet->writeString( $i, $j++, $row['model'] );
        $worksheet->writeString( $i, $j++, $row['name'] );
        $worksheet->writeString( $i, $j++, $row['quantity'] );
        $worksheet->writeString( $i, $j++, $row['price'] );
        $worksheet->writeString( $i, $j++, $row['ptotal'] );
        $worksheet->writeString( $i, $j++, $row['tax'] );
        $worksheet->writeString( $i, $j++, $row['sub_total'] );
        $worksheet->writeString( $i, $j++, $row['store_credit'] );
        $worksheet->writeString( $i, $j++, $row['order_status'] );
        $worksheet->writeString( $i, $j++, $row['store_name'] );

        $this->load->model('catalog/product');
        $product = $this->model_catalog_product->getProduct($row['product_id']);
        $worksheet->writeString( $i, $j++, $product['cost'] );

        $worksheet->writeString( $i, $j++, trim(next(explode('(',$row['coupon_code'])),')') );
        $worksheet->writeString( $i, $j++, $row['coupon_value'] );

        $worksheet->writeString( $i, $j++, trim(next(explode('(',$row['gift_code'])),')') );
        $worksheet->writeString( $i, $j++, $row['gift_value'] );

        $worksheet->writeString( $i, $j++, $row['advanced_coupon_code'] );
        $worksheet->writeString( $i, $j++, $row['ac_amount'] );

        $worksheet->writeString( $i, $j++, $row['sales_promo_code'] );
        $worksheet->writeString( $i, $j++, $row['sales_promo_amount'] );


        // works fine don't need $order_options = array();
        //$order_str = array();
        //$order_options = $this->getOrderOptions($row['order_id'], $row['order_product_id']);
        //foreach($order_options as $order_option){
        //  $order_str[] = $order_option['name'].':'.$order_option['value'];
        //}
        //$worksheet->writeString( $i, $j++, implode('@@@' , $order_str));
        //works fine don't need 

        $i += 1;
        $j = 0;
    }
}

2 个答案:

答案 0 :(得分:0)

尝试替换:

 $worksheet->writeString( $i, $j++, $row['advanced_coupon_code'] );

使用:

 foreach( $row['advanced_coupon_code'] as $code )
 {
   $worksheet->writeString( $i, $j++, $code );
 }

答案 1 :(得分:0)

在您的查询中,您可以替换此部分:

(SELECT title FROM " . DB_PREFIX . "order_total ot WHERE ot.order_id = o.order_id AND ot.code = 'advanced_coupon') AS advanced_coupon_code,

到这个

(SELECT GROUP_CONCAT(`title`) FROM " . DB_PREFIX . "order_total ot WHERE ot.order_id = o.order_id AND ot.code = 'advanced_coupon' GROUP BY ot.order_id) AS advanced_coupon_code,

或者,如果您信任我,只需将我的$query = ". . . ";字符串替换为我的变体:

$query  = "
    SELECT 
        o.*, 
        op.name, 
        op.model, 
        op.order_product_id, 
        op.quantity, 
        op.price, 
        op.total AS ptotal, 
        op.product_id, 
        op.tax, 
        (
            SELECT amount 
            FROM " . DB_PREFIX . "sales_promotion_history 
            WHERE order_id = o.order_id
        ) AS sales_promo_amount, 
        (
            SELECT title 
            FROM " . DB_PREFIX . "order_total ot 
            WHERE ot.order_id = o.order_id AND ot.code = 'sales_promotion'
        ) AS sales_promo_code, 
        (
            SELECT value 
            FROM " . DB_PREFIX . "order_total ott 
            WHERE ott.order_id = o.order_id AND ott.code = 'shipping'
        ) AS shipping_cost, 
        (
            SELECT name 
            FROM " . DB_PREFIX . "order_status os 
            WHERE os.order_status_id = o.order_status_id
        ) AS `order_status`, 
        (
            SELECT ot.value 
            FROM " . DB_PREFIX . "order_total ot 
            WHERE ot.order_id = o.order_id AND ot.code = 'sub_total'
        ) AS sub_total, 
        (
            SELECT ot.value 
            FROM " . DB_PREFIX . "order_total ot 
            WHERE ot.order_id = o.order_id AND ot.code = 'credit'
        ) AS store_credit, 
        (
            SELECT ot.title 
            FROM " . DB_PREFIX . "order_total ot 
            WHERE ot.order_id = o.order_id AND ot.code = 'voucher'
        ) AS gift_code, 
        (
            SELECT ot.text 
            FROM " . DB_PREFIX . "order_total ot 
            WHERE ot.order_id = o.order_id AND ot.code = 'voucher'
        ) AS gift_value, 
        (
            SELECT GROUP_CONCAT(`title`) 
            FROM " . DB_PREFIX . "order_total ot 
            WHERE ot.order_id = o.order_id AND ot.code = 'advanced_coupon' 
            GROUP BY ot.order_id
        ) AS advanced_coupon_code,
        (
            SELECT amount 
            FROM " . DB_PREFIX . "advanced_coupon_history 
            WHERE order_id = o.order_id
        ) AS ac_amount, 
        (
            SELECT ot.title 
            FROM " . DB_PREFIX . "order_total ot 
            WHERE ot.order_id = o.order_id AND ot.code = 'coupon'
        ) AS coupon_code, 
        (
            SELECT ot.text 
            FROM " . DB_PREFIX . "order_total ot 
            WHERE ot.order_id = o.order_id AND ot.code = 'coupon'
        ) AS coupon_value 
    FROM `" . DB_PREFIX . "order` o 
    INNER JOIN `" . DB_PREFIX . "order_product` op ON o.order_id=op.order_id 
    WHERE o.order_id IN ( ".$orderid." ) 
    ORDER BY o.order_id";