0

我有一个查询,它正在数据库中搜索订单。我的 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 个包含 2 个产品的订单 (orderid 12635)。客户 3531 下了 1 个包含 1 个产品的订单(orderid 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 将收到两封单独的电子邮件 - 1 封用于 Product1,1 封用于 Product2)。我想要做的是在同一封电子邮件中使用两种产品向客户 4192 发送电子邮件。

如何改进我的脚本,以便在 orderid 相同时,它将从 orderid 相同的所有行(例如 orderproductname、qty )获取信息,然后在 order id 更改时继续。

4

3 回答 3

0

跟踪您使用的 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-30T04:37:12.443 回答
0

尝试在循环之前添加这段代码:

 $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-30T04:37:21.340 回答
0

我认为最好的选择是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

因此您可以在特定订单的列中以逗号分隔产品名称

于 2012-04-30T04:41:31.287 回答