Hi
This script works fine as it is. But when the entry_suburb field is empty (because they have a short address) it messes up the csv file when I need to import the data. So my question, is it possible to write a if statement or similar to check if the entry_suburb field is empty in each line of the databse, if it is could it be filled with with the next column data entry_city?
Any help would be appreciated.
Thanks,
Phil
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db = 'database';
$file = 'export';
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$replace = array(
'address_book_id' => '',
'customers_name' => 'Customer Job',
'entry_firstname' => 'First Name',
'entry_lastname' => 'Last Name',
'entry_street_address' => 'Billing Address1',
'entry_suburb' => 'Billing Address2',
'entry_city' => 'Billing Address3',
'entry_state' => 'Billing Address4',
'entry_postcode' => 'Billing Address5',
'customers_email_address' => 'Email',
'customers_telephone' => 'Phone'
);
function split_num($num){
$num = substr($num, 0, 5) . ' ' . substr($num, 5);
return $num;
}
$values = mysql_query("SELECT zen_orders.customers_name, zen_address_book.entry_firstname, zen_address_book.entry_lastname, zen_address_book.entry_street_address, zen_address_book.entry_suburb, zen_address_book.entry_city, zen_address_book.entry_state, zen_address_book.entry_postcode, zen_orders.customers_telephone, zen_orders.customers_email_address
FROM (zen_address_book INNER JOIN zen_orders ON zen_address_book.customers_id = zen_orders.customers_id) INNER JOIN zen_customers ON (zen_address_book.address_book_id = zen_customers.customers_default_address_id) AND (zen_address_book.customers_id = zen_customers.customers_id)");
$i=0;
while ($rowr = mysql_fetch_assoc($values)) {
if(!preg_match('#\x20#', $rowr['customers_telephone'], $match)){ // does not find a space...
$rowr['customers_telephone'] = split_num($rowr['customers_telephone']);
}
if($i==0) {
foreach(array_keys($rowr) as $title)
$csv_output .= '"'.str_replace(array_keys($replace), $replace, $title).'",';
$csv_output .= "\n";
}
foreach ($rowr as $key => $value) {
$csv_output .= '"'.$value.'",';
}
$csv_output .= "\n";
$i++;
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;