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 は、2 つの製品を持つ 1 つの注文 (orderid 12635) を作成しました。顧客 3531 は、1 つの製品 (orderid 12636) を持つ 1 つの注文を行いました。

注文ごとに情報を取得してまとめたいと思っています。注文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 用と製品 2 用の 2 つの別々の電子メールを受け取ります)。私がやりたいことは、顧客 4192 に両方の製品を同じ電子メールで送信することです。

orderid が同じである間に、 orderid が同じであるすべての行 (例: orderproductname, qty ) から情報を取得し、注文 ID が変更されたときに続行するようにスクリプトを改善するにはどうすればよいですか。

4

3 に答える 3

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 に答える
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 に答える