Reputation: 5302
I'm creating a csv file via php and I'm getting some values from my database.
I'm getting a value successfully and generating the csv excellently.
I have a value like this 0012345 and 0051234
My problem here is when I view the data in my csv file, the 00 is not displaying only the 12345 and 51234.
When I check on my excel and I go to Format Cells, it seems it is set into General mode. It should be set into Text so that it would accept 00 as the first digit of the number.
This is my code:
$csv_filename="ePay.csv";
header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-Type: application/vnd.ms-excel");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=$csv_filename");
include("/includes/config.php");
$sql = "SELECT * FROM moneyroute where sender_country ='Nigeria' and status = 'new'";
#echo $sql;
$olu="AfrotradeGroup";
$result=mysql_query($sql);
if(mysql_num_rows($result)>0){
$fileContent="Beneficiary Name,Beneficiary Account No,Beneficiary Bank Code,Transaction Amount,Narration\n";
while($data=mysql_fetch_array($result))
{
$name_=$data['sender_first_name']." ".$data['sender_last_name'];
if($data['status']=='u')
$sta='Approved';
else
$sta='Pending';
$fileContent.= "".$data['receiver_first_name']." ".$data['receiver_last_name'].",".$data['accountno'].",".$data['sortcode'].",".$data['equal_currency'].",".$olu."\n";
}
$fileContent=str_replace("\n\n","\n",$fileContent);
echo $fileContent;
}
I'm getting a result like this: Ade you 67654578 97865 30151.52 AfrotradeGroup\ The 67654578 should be 067654578 and 97865 should be 097865
What's the solution for this problem? Thanks in advance and have a great day.
Upvotes: 0
Views: 1527
Reputation: 51
It seems the problem is that you have to use the casting operators.
$myText = (string)$myVar;
This way the variable is used as a text and not as a number.
Upvotes: 1