I am trying to run two mysql queries and export the results into a CSV.
My issue is the second query I have is being written to a new row instead of just onto the same rows that the result from the prior query gets written to.
I also can not join the two queries because the second query I am running might not have a matching row. Below is my code
//Load Magento
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
//database connection
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT sales_flat_order.entity_id, sales_flat_order.created_at, sales_flat_order.increment_id AS OrderID,
sales_flat_order.status, sales_flat_order_address.firstname, sales_flat_order_address.lastname,
sales_flat_order_address.city, sales_flat_order_address.region, sales_flat_order_address.postcode,
sales_flat_order_payment.method AS payment_method, sales_flat_order_payment.base_amount_paid AS pmt, sales_flat_order_payment.base_amount_authorized AS charged,
sales_flat_order_payment.base_shipping_captured AS shipping_invoice
FROM sales_flat_order, sales_flat_order_payment, sales_flat_order_address
WHERE MONTH(sales_flat_order.created_at) = 3 AND sales_flat_order.status IN('complete','closed') AND
sales_flat_order.entity_id = sales_flat_order_payment.entity_id AND sales_flat_order.entity_id = sales_flat_order_address.parent_id";
$list = $connection->fetchAll($sql);
//$headers = array();
$headers = array("entity_id", "created_at", "OrderID", "Status", "firstName", "LastName", "City", "State", "Zipcode", "Pmt Method", "PMT", "Charged", "Shipping Invoice", "TaxCode", "TaxRate", "Tax Amt");
$fp = fopen('php://output', 'w');
if ($fp && $list)
{
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="salestax_report.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
foreach ($list as $fields)
{
fputcsv($fp, $fields);
$entity_id = $fields['entity_id'];
$sqlb = "SELECT code AS taxcode, percent AS taxrate, amount as taxamount FROM sales_order_tax
WHERE order_id = $entity_id";
$listb = $connection->fetchAll($sqlb);
foreach ($listb as $fieldsb)
{
fputcsv($fp, $fieldsb);
}
}
die;
}