A comma separated value
(CSV) is a common format for exporting and importing data from the
database and it is a widely supports of specific web and desktop
application for exporting a bunch of data and exporting to the other
application, and off course same table structure for not returning error
for importing.
In this tutorial, we create small a php application can be able to export the data from database table to cvs. A page listed of data from the database table, in a list we can choose a individual data to export or if you all data export you can be able check all and export in one click. let’s begin.
In this tutorial, we create small a php application can be able to export the data from database table to cvs. A page listed of data from the database table, in a list we can choose a individual data to export or if you all data export you can be able check all and export in one click. let’s begin.
Step 1: Create a Database
First create a database named ‘export_csv’ you can change if you want, and create a table named ‘csv_data’ and fields, and insert some data.
Step 2: Create Database Connection
Create file name ‘database.php’ to connect from the database we have assigning the database credentials, MySQL host, user, password and database name the ‘import_csv’.
Step 3: Creating the Page Template
Create a file name ‘index.php’ in the page we have a list of sample products and check boxes can be to able to choose a product to import, and we need to create a jQuery function can be able to check all in one click to import all products.
index.php
A simple style for the page.
style.css
script.js
In the jQuery script we assign the id attribute to the check box in the header of the table and triggered to click to check all the check boxes, we count and loop the check boxes and get each attribute type, if the attribute type is checkbox it will checked.
Step 4: The Export PHP Script
export_csv.php
In the php export script we require the database connection first and the define the database name and table name and post submit. We loop first the table fields and con cat to comma and same for the products. In the output we set the file name as time function to generate numbers.
Step 5: Complete
Were done, we created a php web application can be able to export the data to csv. Let’s have a look at what we’ve achieved:
- We’ve set up a database.
- We’ve create page to fetch the data.
- We’ve write the jQuery script for check all function.
- We’ve write the php script for to generated the csv.
If you enjoyed this article, please consider sharing it!
No comments:
Post a Comment