Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
987 views
in Technique[技术] by (71.8m points)

mysql - Creating a CSV file with PHP

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
";
        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."
";
    }


$fileContent=str_replace("

","
",$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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...