我有一个查询,它在数据库中搜索订单。我的SQL查询返回订单信息,其中订单id是关键字(对于包含多个产品的订单,id是重复的),以及附加在其上的订单信息,包括产品数量和变体。
该查询连接几个表并输出,如下所示
|--orderid--|--orderstatus--|--ordcustid--|--orderprodname--|--orderprodqty--|--orderqty--|
|---12635---|-----11--------|-----4192----|----Product1-----|--------1-------|-----2------|
|---12635---|-----11--------|-----4192----|----Product2-----|--------1-------|-----2------|
|---12636---|-----11--------|-----3531----|----Product3-----|--------1-------|-----1------|正如你在上面看到的。客户4192已经下了1个订单(订单has 12635),该订单有2个产品。客户3531已经下了1个订单,该订单有1个产品(订单has 12636)。
我想要捕获每个订单的信息并将其放在一起。因此,虽然订单id是相同的,但我正在收集信息。
我当前的脚本如下所示,但它只适用于每一行。
$result = mysql_query("
SELECT *
FROM orders, orderproducts, productimage, customers
WHERE orders.orderid = orderproducts.orderorderid
AND orderproducts.orderprodid = productimage.imageprodid
AND orders.ordcustid = customers.customerid
AND ordstatus = '11'
ORDER BY orderid
");
while($row = mysql_fetch_array($result))
{
//do stuff
}该脚本的目的是通过邮件发送每个订单的信息。我当前的脚本所做的是为每个产品向客户发送邮件(即客户4192将收到两封单独的电子邮件-一封用于Product1,一封用于Product2)。我想做的是给客户4192发电子邮件,两个产品都在同一封电子邮件上。
我如何改进我的脚本,当orderid相同时,它将从orderid相同的所有行中获取信息(例如orderproductname,qty ),然后在order id更改时继续。
发布于 2012-04-30 12:37:21
试着在你的循环之前添加这段代码:
$last_id = 0;在while()循环中:
if ( $last_id != $row['orderid'] ) {
// New Order - do some initiation stuff
$last_id = $row['orderid'];
} else {
// Continuing the current order - do some different stuff
}下面是完整的代码:
$result = mysql_query("
SELECT *
FROM orders, orderproducts, productimage, customers
WHERE orders.orderid = orderproducts.orderorderid
AND orderproducts.orderprodid = productimage.imageprodid
AND orders.ordcustid = customers.customerid
AND ordstatus = '11'
ORDER BY orderid
");
$last_id = 0;
while($row = mysql_fetch_array($result)) {
if ( $last_id != $row['orderid'] ){
// Initiate a new order
$message_body = ''; // Reset message body for each order
$last_id = $row['orderid']; // Keep track of the orderid last used
if ( $last_id > 0 ){
// Make certain we didn't just begin the loop, then email the customer
echo "SAMPLE MESSAGE BODY FOR ORDER $last_id <br/>\r\n";
echo $message_body;
// Uncomment following lines to send an email
// $headers = $headers = 'From: My Store <info@mystore.com>\r\nContent-Type: text/html; charset=ISO-8859-1\r\n";
// $success = mail( 'customer@email.com', 'Order Confirmation', $message_body, $headers);
}
} else {
// Step through current order
// Add a line to the message body for each product record
$message_body .= "Product {$row['orderprodname']}, Qty: {$row['orderprodqty']} <br/>\r\n";
}
}发布于 2012-04-30 12:37:12
跟踪你在哪个orderID上,并建立一封电子邮件,直到orderID发生变化。当它发生时,您发送电子邮件,并为新订单启动一个新的电子邮件。例如:
$cur_order = null;
$order_data = array();
while($row = mysql_fetch_assoc($result)) {
if ($cur_order !== $row['orderid']) {
sendOrderEmail($order_data);
$cur_order = $row['orderid'];
$order_data = array();
}
$order_data[] = $row;
}发布于 2012-04-30 12:41:31
我认为最好的选择是在查询本身中使用group_concat of product names。例如,
SELECT *,group_concat(orderprodname)
FROM orders, orderproducts, productimage, customers
WHERE orders.orderid = orderproducts.orderorderid
AND orderproducts.orderprodid = productimage.imageprodid
AND orders.ordcustid = customers.customerid
AND ordstatus = '11' group by orderid
ORDER BY orderid因此,您可以使用逗号分隔产品名称,并将其放在特定订单的列中
https://stackoverflow.com/questions/10378533
复制相似问题