remedy_rider
asked on
Replacing line breaks before outputing to excel?
Hello, I found a good script to export orders from Magento but when someone has address1 and address2 filled in, it combines them in the table as "street". It must be putting a line break or some charater i can't see, because the excel file is messed up. I attached an export to see what i'm talking about. See line 8
I'm trying to figure out how to strip the "street" column of breaks (if that's what's even happening?)
Thanks in advance.
I'm trying to figure out how to strip the "street" column of breaks (if that's what's even happening?)
Thanks in advance.
<?
	//
	// Magento CSV Report Script - Unprocessed Sales Orders
	//
	
	// DATABASE CONNECTION
	
	
	$hostname_MySQLCon="localhost";
	$database_MySQLCon = "db";
	$username_MySQLCon = "user";
	$password_MySQLCon = "password";
	$MySQLCon = mysql_pconnect($hostname_MySQLCon, $username_MySQLCon, $password_MySQLCon);
	$MySQLDb = mysql_select_db($database_MySQLCon, $MySQLCon);
	
	// FUNCTION AREA
	
	function get_DB_Row($query){
		$result = @mysql_query($query);
		$numresults = @mysql_num_rows($result);
		
		for ($i = 0; $i < $numresults; $i++)
		{
			$row = @mysql_fetch_array($result);
		}
		
		return $row;
	}
	
	// Using this temporary function until I find correct way to pull country names from the database according to two digit code
	// Put in your own until a database polled solution is uncovered
	
	function getCountryName($code){
		switch($code) {
			case "GB" 	:	$country = "United Kingdom";
							break;
			case "IE" 	:	$country = "Ireland";
							break;
		}
		
		return $country;
	}
	
	// INITILISATION AREA
	
	$sitename = "boyesen.com"; 				// update this to customise your filename
	$datestring = date('j-m-y');
	$filename = $sitename."-".$datestring;
	$output = "";
	
	$fieldlist = array("orderid","firstname","lastname","address","city","region","country",
	"postcode","telephone","email","itemcode","itemname","quantity","shipping_amount","shipping_instructions","order_date");
	
	$checklist = array("country", "quantity");
	
	// FIELDLIST INSTRUCTIONS
	// The order of the CSV fields are set here as well as the field titles
	// The field titles must correspond to a number of SQL Variables being set with the AS operator below
	// If you change the default titles be sure to also update the SQL command accordingly
	// Please note that 'country' and 'quantity' values must be processed but should still be put in your prefered sequence
	
	$numfields = sizeof($fieldlist);
	
	// ********************* NOW START BUILDING THE CSV
	
	// Create the column headers
	
	for($k =0; $k < $numfields; $k++) {
		$output .= $fieldlist[$k];
		if ($k < ($numfields-1)) $output .= ", ";
	}
	$output .= "\n";
	
	
	$orderquery = "SELECT * FROM sales_order"; 	// Query to find unprocessed orders
	$orderresult = mysql_query($orderquery);
	$numorders = @mysql_num_rows($orderresult);
	
	for($i =0; $i < $numorders; $i++) {
		$order = @mysql_fetch_array($orderresult);	// Place each order into an array for proccessing
		$orderid = $order['entity_id']; 			// Grab the orderid for use in the main SQL command
		
		// The following SQL command will find all the individual items associated with this order
		
		$itemquery = 	"SELECT sales_flat_order_item.order_id AS orderid,
						sales_flat_order_item.product_type,
						sales_flat_order_item.quote_item_id,
						sales_flat_order_item.parent_item_id AS parentid,
						sales_flat_order_item.sku AS itemcode,
						sales_flat_order_item.name AS itemname,
						sales_flat_order_item.qty_ordered AS qty_ordered,
						sales_flat_quote_address.shipping_amount AS shipping_amount,
						sales_flat_order_item.qty_shipped AS qty_shipped,
						sales_flat_quote_address.email AS email,
						sales_flat_quote_address.prefix AS title,
						sales_flat_quote_address.firstname AS firstname,
						sales_flat_quote_address.lastname AS lastname,
						sales_flat_quote_address.street AS address,
						sales_flat_quote_address.city AS city,
						sales_flat_quote_address.region AS region,
						sales_flat_quote_address.country_id AS country,
						sales_flat_quote_address.postcode AS postcode,
						sales_flat_quote_address.telephone AS telephone,
						sales_flat_quote_address.shipping_description AS shipping_instructions,
						sales_flat_quote_address.created_at AS order_date
						FROM sales_flat_order_item
						INNER JOIN sales_flat_quote_item
						ON sales_flat_order_item.quote_item_id = sales_flat_quote_item.item_id
						INNER JOIN sales_flat_quote_address
						ON sales_flat_quote_item.quote_id = sales_flat_quote_address.quote_id
						WHERE sales_flat_order_item.order_id = $orderid
						AND sales_flat_quote_address.address_type = 'shipping'
						AND sales_flat_order_item.product_type <> 'configurable'
						AND sales_flat_order_item.qty_shipped < sales_flat_order_item.qty_ordered";
						
		$itemresult = mysql_query($itemquery);		// Run the query
		$numitems = @mysql_num_rows($itemresult); // Check the number of items in the order
		
		for($j =0; $j < $numitems; $j++) {
			$item = @mysql_fetch_array($itemresult); // Place the item in an array for processing
			
			// Output the order item values in the same sequence set in the fieldlist to match headers
			
			for($m =0; $m < sizeof($fieldlist); $m++) {
				
				$fieldvalue = $fieldlist[$m];
				
				if(in_array($fieldvalue, $checklist)) { 				// check if on special processing list
				
					if($fieldvalue == "country") $output .= getCountryName($item[$fieldvalue]);
					
					if($fieldvalue == "quantity") {
						$parentid = $item['parentid'];
						if($parentid == 'NULL') { 						// simple product sold on its own (no parent)
							$qty_ordered = $item["qty_ordered"];
							$qty_shipped = $item["qty_shipped"];
						} else { 										// simple product was part of bundle or configurable group
																		// If so, use the parent quanities to calculate
							$parentitem = get_DB_row("SELECT * FROM sales_flat_order_item WHERE item_id = $parentid");
							$qty_ordered = $parentitem["qty_ordered"];
							$qty_shipped = $parentitem["qty_shipped"];
						}
						$output .= ($qty_ordered - $qty_shipped);
					}
					
				} else {
					$output .= $item[$fieldvalue];
				}
				
				if ($m < ($numfields-1)) $output .= ", ";
			}
			
			$output .= "\n";
		}
		
		
	}
	
	// Send the CSV file to the browser for download
	
	header("Content-type: text/x-csv");
	header("Content-Disposition: attachment; filename=$filename.csv");
	echo $output;
	exit;
	
?>
testexport-1-09-10.xls
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
GEEEZE i'm flakin here! can't believe i left those in the code. thanks for pointing that out Ray!
How do i add the double quotes when it's coming into an array. Sorry i'm a php novice. I was trying to find where it inserts via magento, but that's like finding a needle in a haystack.
How do i add the double quotes when it's coming into an array. Sorry i'm a php novice. I was trying to find where it inserts via magento, but that's like finding a needle in a haystack.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, i'm still not sure what the best way to alot points is? Marbleman's fix worked like a charm, but everyone elses input pointed me to some good info.
ASKER
thank you modus. that's what i get for being in a hurry. arrgh.
You can (and should) use var_dump() to print out variables when you're not sure what they contain. It will reveal a multitude of valuable information.
You can (and should) normalize the addresses in your data base. You can do this with a call to the Google Geocoder. It will return the correct, normalized address.
This function is how to write a CSV correctly:
http://us3.php.net/manual/en/function.fputcsv.php
You might also want to ask a moderator to remove your credentials from the code snippet.
HTH, ~Ray