0

好的,我有这个正在运行的 CSV 导出脚本,但我想添加另一个查询,该查询将获取每个订单的订单项目。

并将每个订单的订单项放在每行的最后一个单元格中,如下所示:

Order_Items:项目 1、项目 2、项目 3 等...

我有订单项目的查询,但我不确定如何将它与复杂的 while 和 for each 循环相匹配。

这是我到目前为止的代码。

  <?php
  include "../Config/db.config.php";;

 // Make a MySQL Connection
 $link =  mysql_connect(EZSQL_DB_HOST, EZSQL_DB_USER, EZSQL_DB_PASSWORD);

 if (!$link) {
    die('Could not connect: ' . mysql_error());
 }

if (!mysql_select_db(EZSQL_DB_NAME, $link)) {
   echo 'Could not select database';
  exit;
}

 $select = "SELECT intOrderID as Order_ID, strPaymentMethod as Payment_Method, strPaymentRef as Payment_Ref, strPaymentStatus as Payment_Status,
       dtmDate as Date, curPaymentAmount as Net_Price, curShippingFee as Shipping_Fee FROM tbl_mod_ShopOrders";

$items = "SELECT intProductID, strProductTitle, curPrice, intQty FROM tbl_mod_ShopOrderItems WHERE intOrderID = $value";


$export = mysql_query ($select) or die ( "Sql error : ". mysql_error() );

$fields = mysql_num_fields ( $export );

for ( $i = 0; $i < $fields; $i++ )
{
   $header .= mysql_field_name( $export , $i ) . ",";  
}

$header .= 'Order_Items'; // Added additional cell title. 

while( $row = mysql_fetch_row( $export ) )
{
   $line = '';
foreach( $row as $value )
{                                            
    if ( ( !isset( $value ) ) || ( $value == "" ) )
    {
        $value = ",";
    }
    else
    {
        $value = str_replace( '"' , '""' , $value );
        $value = '"' . $value . '"' . ",";
    }
    $line .= $value;
}
    $data .= trim( $line ) . "\n";
 }
    $data = str_replace( "\r" , "" , $data );

 if ( $data == "" )
 {
     $data = "\n(0) Records Found!\n";                        
}

 header("Content-type: application/octet-stream");
 header("Content-Disposition: attachment; filename=Customer_export.csv");
 header("Pragma: no-cache");
 header("Expires: 0");
 print "$header\n$data";
 exit;
4

1 回答 1

0

我在这里不是 100% 的查询,它可能需要调整才能得到你想要的。我觉得这里重要的是查询,您可以让 MySQL 为您完成大部分繁重的工作。我在fputcsv()这里使用了格式化数据的方法,因为它会产生最好的结果。我也不愿意使用or die(),但由于这是您在上述代码中处理错误的方式,所以这就是我在这里所做的。

希望这会给您一个不错的起点:

<?php

  // Make a MySQL Connection
  include "../Config/db.config.php";;
  $link = mysql_connect(EZSQL_DB_HOST, EZSQL_DB_USER, EZSQL_DB_PASSWORD)) or die('Could not connect: '.mysql_error());
  mysql_select_db(EZSQL_DB_NAME, $link)) or die('Could not select database: '.mysql_error($link));

  // Get data from database
  $query = "
    SELECT
      `o`.`intOrderID` AS 'Order_ID',
      `o`.`strPaymentMethod` AS 'Payment_Method',
      `o`.`strPaymentRef` AS 'Payment_Ref',
      `o`.`strPaymentStatus` AS 'Payment_Status',
      `o`.`dtmDate` AS 'Date',
      `o`.`curPaymentAmount` AS 'Net_Price',
      `o`.`curShippingFee` AS 'Shipping_Fee',
      `i`.`Order_Items`
    FROM `tbl_mod_ShopOrders` `o`
    LEFT JOIN (
      SELECT
        `intOrderID`,
        GROUP_CONCAT(CONCAT('ID=', `intProductID`, '; Title=', `strProductTitle`, '; Price=', `curPrice`, '; Qty=', `intQty`) SEPARATOR '\\n') AS 'Order_Items'
      FROM `tbl_mod_ShopOrderItems`
      GROUP BY `intOrderID`
    ) `i` ON `o`.`intOrderID` = `i`.`intOrderID`
  ";
  $result = mysql_query($query) or die("Query error: ".mysql_error($link));

  // Check there were some results
  mysql_num_rows($result) or die('No data to output');

  // Open fake file pointer to output buffer
  $outFp = fopen('php://output', 'w') or die('Unable to open file pointer for output buffer');

  // Send headers for CSV download
  header("Content-Type: text/csv");
  header("Content-Disposition: attachment; filename=Customer_export.csv");
  header("Pragma: no-cache");
  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

  // Output columns headers and first row
  $row = mysql_fetch_assoc($result);
  fputcsv($outFp, array_keys($row));
  fputcsv($outFp, $row);

  // Output rest of data...
  while ($row = mysql_fetch_row($result)) {
    fputcsv($outFp, $row);
  }

  // ...and we're done.
  exit;

请注意,这LEFT JOIN可以说是不必要的,因为您永远不会收到没有任何物品的订单,但为了安全也是如此。

于 2012-04-20T13:06:43.507 回答