Today, I want to talk about how you would go about creating a CSV file from scratch, with all of your data from the database.
First off we need a little PHP code, very simple and hopefully you can understand what it is doing. To create the file we are going to be using the fopen() to check if a file is there and writable, and if it is not there we want to create it.
So above you see the variable, $csv_fields, and it hasn’t been declared. This is the variable that I am using to store all of my data from the data base in. Remember this needs to be declared BEFORE the code above, I just wanted to break it up for a little easier viewing of both. Below we start the array() variable $csv_fields, and add the header of the CSV file, this is not a needed step at all, but if you want to have the top of the columns labeled, this is how it is done.
Now we want to actually have the data into the variable $csv_fields.. Next does just that.