I have a query which is searching a database for orders. My SQL query returns order information with the order id being the key (the id is repeating for orders with more than one product) and order information attached to that including product qty and variation.
The query joins a few tables and outputs as follows
|--orderid--|--orderstatus--|--ordcustid--|--orderprodname--|--orderprodqty--|--orderqty--|
|---12635---|-----11--------|-----4192----|----Product1-----|--------1-------|-----2------|
|---12635---|-----11--------|-----4192----|----Product2-----|--------1-------|-----2------|
|---12636---|-----11--------|-----3531----|----Product3-----|--------1-------|-----1------|
As you can see above. Customer 4192 has made 1 order (orderid 12635) that has 2 products. Customer 3531 has made 1 order that has 1 product (orderid 12636).
I’m wanting to capture the information for each order and put it together. So while the order id is the same I’m collecting the information.
My current script looks like this, but it just works on every row.
$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
}
The purpose of the script is to mail the information for each order. What my current script is doing is mailing the customer for every product (i.e. customer 4192 would get two separate emails – 1 for Product1 and 1 for Product2). What I’m wanting to do is email customer 4192 with both products on the same email.
How can I improve my script so that while the orderid is the same it will get information from all of the rows where the orderid is the same (eg orderproductname, qty ) and then continue when the order id changes.
Try adding this bit of code before your loop:
And in your while() loop:
Here’s the code altogether: