0

I don't know this question is asked or not. I didn't get his in suggestion.

I'm trying to write some data from database to an csv file. I'm using fputcsv function.

I'm using following code :

$fh = @fopen($fileName, 'w+');
        $writeText['sku'] = $somevalue
        $writeText['attribute_set'] = $somevalue
        $writeText['type'] = $somevalue
        $writeText['default_catalogue_link']= $somevalue
        $writeText['status'] = $somevalue
@fputcsv($fh, $writeText);
        @flush();
@fclose($fh);

Some of the values starts with zero like $writeText['sku'] is 0987. After writing to the file. I'm getting 987 in the file.

how to prevent this? I need with zero values.

Any help would be usefull. Thanks in advance.

7
  • Try $writeText['sku'] = (string)$somevalue; Commented Feb 23, 2015 at 6:47
  • If that doesn't work and you know that all skus are 4 digits you could use str_pad() to maintain a leading zero. Or I believe if you ensure that the values are encapsulated as strings (with quotes) in the CSV they should keep the leading zeros. Commented Feb 23, 2015 at 6:50
  • (string)somevalue is not working. Not all the values are of 4digits Commented Feb 23, 2015 at 6:53
  • Eek, you'll probably need to make sure the CSV has the numbers encapsulated in quotes then. Commented Feb 23, 2015 at 7:43
  • I tried $writeText['sku'] = " ".$somevalue; also, its not working. I'm not getting how to encapsulate with qoutes :-( Commented Feb 23, 2015 at 7:45

1 Answer 1

1

Are you using Excel to open the file? Because Excel interprets it as a number. Try open and see with a text editor.

Sign up to request clarification or add additional context in comments.

3 Comments

nothing wrong with the code? check all those error supression operators! -.-
sorry, i was trying to say that the data might still be in the csv even if he doesn't see it in the correct format. For an example I once tried to get the string "536775212172206080" on a csv and it was showing 5.37E+17 but the number was still there when I check with a text editor.
@Sanura Yes, you are right. It's showing properly in text editor, I wanted use that file as input for another application, Its working fine. :-) Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.