【问题标题】:PHP mySQL result array - while a row's id remains the same do something, when the row's id changes move onPHP mySQL 结果数组 - 当行的 id 保持不变时做某事,当行的 id 更改继续
【发布时间】:2012-04-30 04:33:58
【问题描述】:

我有一个查询,它正在数据库中搜索订单。我的 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 个产品的订单(订单 ID 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 更改时继续。

【问题讨论】:

    标签: php mysql


    【解决方案1】:

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

     $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";
        }
    }
    

    【讨论】:

    • 您能否详细说明一下入门内容?我只是一个 php 初学者,对数组没有太多经验。
    • 当然!启动代码可以是任何东西——也许是检索客户姓名和电子邮件的查询,也许是重置一些数据变量。除了设置$last_id 变量之外,您可能不需要做任何事情。每次订单更改时都会执行此处的代码,因此您需要在此处处理订单电子邮件。我将更新我的示例代码,为您提供更详尽的示例。
    【解决方案2】:

    跟踪您使用的 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;
    }
    

    【讨论】:

    • 为了测试这个方法,我已经尝试过了,但是它们仍然出现在不同的行中:$cur_order= null; $order_data= array(); while($row = mysql_fetch_array($result)) { if ($cur_order !== $row['orderid']) { $to = $row['customeremail']; $products = $row['orderprodname']; echo $to . " Your Products: " . $products . "&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;"; } $order_data[] = $row; }
    【解决方案3】:

    我认为最好的选择是在查询本身中使用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
    

    这样您就可以在特定订单的列中以逗号分隔产品名称

    【讨论】:

    • 这是返回 #1064 sql 错误... #1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 2 行的 'group_concat(orderprodname) WHERE orders.orderid = orderproducts.orderorderi' 附近使用正确的语法
    • 我玩过它,这很有效,但是我不确定它是否能让我得到最终结果。 SELECT orderid, GROUP_CONCAT( orderproducts.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
    • @James :是的,我更改了查询。我希望您可以从该查询中获得所有必要的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多