好的,我有这个正在运行的 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;