PHP mySQL结果数组-当一行的id保持不变时,当该行的id发生变化时,继续执行操作
我有一个查询正在数据库中搜索订单。我的SQL查询返回订单信息,订单id为键,该id针对包含多个产品的订单重复,订单信息包括产品数量和变化 查询连接了几个表和输出,如下所示PHP mySQL结果数组-当一行的id保持不变时,当该行的id发生变化时,继续执行操作,php,mysql,Php,Mysql,我有一个查询正在数据库中搜索订单。我的SQL查询返回订单信息,订单id为键,该id针对包含多个产品的订单重复,订单信息包括产品数量和变化 查询连接了几个表和输出,如下所示 |--orderid--|--orderstatus--|--ordcustid--|--orderprodname--|--orderprodqty--|--orderqty--| |---12635---|-----11--------|-----4192----|----Product1-----|--------1--
|--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个订单,订单ID 12635包含2个产品。客户3531下了一个订单,该订单的产品订单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封用于产品1,1封用于产品2。我想做的是在同一封电子邮件中向客户4192发送两种产品的电子邮件
如何改进我的脚本,以便在orderid相同的情况下,它可以从orderid相同的所有行(例如orderproductname、qty)中获取信息,然后在订单id更改时继续操作。跟踪您使用的orderid,并建立电子邮件,直到orderid更改为止。当它出现时,您发送电子邮件,并为新订单启动一个新的电子邮件。e、 g
$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;
}
尝试在循环之前添加以下代码:
$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";
}
}
我认为最好的选择是在查询本身中使用产品名称的group_concat。比如说,
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
因此,您可以在特定订单的列中以逗号分隔产品名称来测试此方法。我已经尝试了此方法,但它们仍然出现在单独的行中:$cur_order=null$顺序\数据=数组;虽然$row=mysql_fetch_array$result{if$cur_order!=$row['orderid']{$to=$row['customeremail'];$products=$row['orderprodname'];echo$to。您的产品:.$products.}$order_data[]=$row;}您能在初始化的内容中再详细一点吗?我只是一个php初学者,对数组没有太多经验。当然!启动代码可以是任何东西——可能是检索客户姓名和电子邮件的查询,也可能是重置一些数据变量。除了设置$last_id变量外,您可能不需要执行任何操作。这里的代码在每次订单更改时都会执行,因此这是您希望处理订单电子邮件的地方。我将更新我的示例代码,为您提供一个更全面的示例。这将返回1064个sql错误。。。1064-您的SQL语法有错误;查看与MySQL服务器版本对应的手册,了解第2行“group_concatorderprodname WHERE orders.orderid=orderproducts.orderorderi”附近使用的语法是否正确。我使用了它,但我不确定它是否能得到最终结果。从orders、orderproducts、productimage、,orders.orderid=orderproducts.orderorderid和orderproducts.orderprodid=productimage.imageprodid和orders.ordcustid=customers.customerid和ordstatus='11'按orderid分组orderid@James:是的,我更改了查询。我希望您可以从此查询本身获得所有必要的信息。