orders_id, orders_products_id, products_id, products_name, products_model, products_price, products_tax
2 2 2 2 134 134 "Item Name"
And maybe it's not duplicating stuff - it's just getting everything screwed up somewhere between "Iterate through and get every column's name" and "Post this record into the CSV". But I don't see where I'm doing any actions to the result that would result in the data being moved around. Any advice would be greatly appreciated.
<?php
// SQL statement to select all the necessary information, this is the stuff that nightmares are made of...
$sql = "SELECT zen_orders_products.orders_id, zen_orders_products.orders_products_id, zen_orders_products.products_id, zen_orders_products.products_name,
zen_orders_products.products_model, zen_orders_products.products_price, zen_orders_products.products_tax,
zen_orders_products.products_quantity, zen_orders_products.final_price,
zen_orders_products.onetime_charges,
zen_orders_products.products_priced_by_attribute, zen_orders_products.product_is_free, zen_orders_products.products_discount_type,
zen_orders_products.products_discount_type_from, zen_orders.orders_id, zen_orders.customers_id, zen_orders.customers_name, zen_orders.customers_company,
zen_orders.customers_street_address, zen_orders.customers_suburb, zen_orders.customers_city,
zen_orders.customers_postcode, zen_orders.customers_state, zen_orders.customers_country,
zen_orders.customers_telephone, zen_orders.customers_email_address, zen_orders.customers_address_format_id,
zen_orders.delivery_name, zen_orders.delivery_company, zen_orders.delivery_street_address, zen_orders.delivery_suburb,
zen_orders.delivery_city, zen_orders.delivery_postcode, zen_orders.delivery_state, zen_orders.delivery_country,
zen_orders.delivery_address_format_id, zen_orders.billing_name, zen_orders.billing_company,
zen_orders.billing_street_address, zen_orders.billing_suburb, zen_orders.billing_city, zen_orders.billing_postcode,
zen_orders.billing_state, zen_orders.billing_country, zen_orders.billing_address_format_id,
zen_orders.payment_method, zen_orders.payment_module_code, zen_orders.shipping_method, zen_orders.shipping_module_code,
zen_orders.coupon_code, zen_orders.cc_type, zen_orders.cc_owner, zen_orders.cc_number, zen_orders.cc_expires, zen_orders.currency, zen_orders.currency_value,
zen_orders.date_purchased, zen_orders.orders_status, zen_orders.last_modified, zen_orders.order_total, zen_orders.order_tax, zen_orders.ip_address, zen_customers.storeNumber
FROM zen_orders_products LEFT JOIN zen_orders
ON zen_orders_products.orders_id = zen_orders.orders_id
JOIN
zen_customers
ON zen_orders.customers_id=zen_customers.customers_id;";
/** Database Stuff **************************************************************************
********************************************************************************************/
$db = mysql_connect('myhost', 'myname', 'mypass') or die("Can't connect"); // Database connection
mysql_select_db('mydatabase'); // Database selection
/** Variables *******************************************************************************
********************************************************************************************/
$result = mysql_query($sql); // Assign the result of the monstrous SELECT statement to $result
$num_fields = mysql_num_fields($result); // Used to put column headings in the CSV file
$fp = fopen('myfile.csv', 'w'); // Opening the file to write
/** Getting the array of column headings to put in the CSV file by iterating through the ***
*** fields, returning the name, and assigning it to the current ($i) element of the array ***
*********************************************************************************************/
for($i = 0; $i < $num_fields; $i++) {
$str[] = mysql_field_name($result, $i);
}
/** Putting the $str which now contains the column headings on the first line of the CSV ****/
fputcsv($fp, $str);
/** Iterating through the $result of the SQL statement, and assigning each result to an
element in the $list array *****************************************************************/
while($row = mysql_fetch_array($result)) {
$list[] = $row;
}
/** Iterating through the $list, which we have nicknamed to $fields, and putting each element
*** into the CSV file. This could very likely be done above I think ***********************/
foreach($list as $fields) {
fputcsv($fp, $fields);
}
/** Close the file - even though it will close automagically, good manners are good ********/
fclose($fp);
?>

New Topic/Question
Reply



MultiQuote





|