Forum Moderators: coopster

Message Too Old, No Replies

how two display 2 similar recordsets in one table

how two display 2 similar recordsets in one table

         

phpbeg

9:59 pm on May 12, 2009 (gmt 0)

10+ Year Member



Hello everyone, I am creating a sales by products report and I am stuck in creating the main table

basically what I need is this

product ID ¦ YTD units ¦ MTD units ¦ Las update
28737 2 1 2009-05-12
28630 1 1 2009-05-12

I have two similar queries,

One to calculate the YTD units

SELECT sales.idsales, sales.date_sales, products.idProducts, SUM(receipts.qty_receipt)
FROM sales, products, receipts
WHERE Year(date_sales)=Year(curdate()) AND receipts.idsales=sales.idsales AND products.idProducts=receipts.idProducts
GROUP BY products.idProducts

And one to calculate the MTD units

SELECT sales.idsales, sales.date_sales, products.idProducts, SUM(receipts.qty_receipt)
FROM sales, products, receipts
WHERE MONTH(date_sales)=MONTH(curdate()) AND receipts.idsales=sales.idsales AND products.idProducts=receipts.idProducts
GROUP BY products.idProducts

Does anyone know how to combine those two queries to be able to display them horizontaly in the same table? So I can show the sales per year and sales per month for each product?

Thank you

mooger35

3:07 pm on May 13, 2009 (gmt 0)

10+ Year Member



I would place the info into an array and then print the array.

basic idea:

$array = array(); // initialize array

while($row_ytd = mysql_fetch_assoc($query1)) {
$array[$row_ytd['product_id']]['ytd'] = $row_ytd['ytd'];
$array[$row_ytd['product_id']]['last_update'] = $row_ytd['last_update'];
}
while($row_mtd = mysql_fetch_assoc($query2)) {
$array[$row_mtd['product_id']]['mtd'] = $row_mtd['mtd'];
}

from there use foreach [php.net] to put the results into a table