好的我有这个CSV导出脚本正在运行,但我想添加另一个查询,它将获取每个订单的订单项。
并将每个订单的订单项放在每行的最后一个单元格中,如下所示:
ORDER_ITEMS: Item1,items2,item3等...
我有订单商品的查询,但我不知道如何适应复杂的while和每个循环。
这是我到目前为止的代码。
<?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;
答案 0 :(得分: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;
N.B。 LEFT JOIN
可能是不必要的,因为你永远不会有没有任何物品的订单,但它也是安全的。