Jump to content

if statement


confused_aswell

Recommended Posts

Hi

 

If my database holds a telephone number that is all numbers like this 01234567890 or 07768743453 the excel file what will display that number will strip the 0 off the front of the number. So my question is can you write a if statement so that if the number in the database is all one long line of numbers, you can add a space after the first 5 numbers to stop the excel file stripping the 0 from the beginning?

 

I think an if statement would do this, but if you know better then please let me know.

 

Thanks,

 

Phil

 

Here is the script that it has to work with -

 

<?php
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db = 'database_name';
$table = 'zen_address_book,zen_customers';
$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' => '', 
'entry_company' => 'Customer Name',
   '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'
); 

$values = mysql_query("SELECT
a.entry_company, a.entry_firstname, a.entry_lastname, a.entry_street_address,
a.entry_suburb, a.entry_city, a.entry_postcode, c.customers_email_address, c.customers_telephone
FROM
zen_address_book as a
LEFT JOIN
zen_customers as c
ON
c.customers_id = a.customers_id 


");

$i=0;
while ($rowr = mysql_fetch_assoc($values)) {
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;

?>

Link to comment
Share on other sites

I think this will work (no way to tell):

<?php
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db = 'database_name';
$table = 'zen_address_book,zen_customers';
$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' => '',
'entry_company' => 'Customer Name',
'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 = preg_replace("[^0-9]",'',$num);
$s1 = substr($num,0,5);
$s2 = substr($num,5,11);
$num = "$s1 $s2";
return($num);
}
$values = mysql_query("SELECT
a.entry_company, a.entry_firstname, a.entry_lastname, a.entry_street_address,
a.entry_suburb, a.entry_city, a.entry_postcode, c.customers_email_address, c.customers_telephone
FROM
zen_address_book as a
LEFT JOIN
zen_customers as c
ON
c.customers_id = a.customers_id 


");

$i=0;
while ($rowr = mysql_fetch_assoc($values)) {
if($i==0) {
	$rowr['customers_telephone'] = split_num($rowr['customers_telephone']);
	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;

?>

Link to comment
Share on other sites

first, please, oh please remove that link! you are giving the world a free list of names, numbers, addresses, and phone numbers of some people in that file.

 

Next, it appears to have worked on some, but not all. hmm...unfortuately, it's time for me to go for the day (I'll try to get on later tonight), so someone else is going to have to look at this.

Link to comment
Share on other sites

Ok

 

I am in the UK so it's getting late too. I have removed the file from the server so the link will not work anymore.

 

Thanks for trying.

 

The Numbers that are correct are the numbers that were entered with a space, so it is the numbers that are entered without a space that are causing the trouble.

 

Speak tomorrow evening UK time.

 

Phil

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.