I created a web application and I want to add a feature like user can export selected records…
Check Box Name Email
Sumit sumit@gmail.com
Karan karan@gmail.com
I want to export this records into an excel file, but only the selected records should be exported.
Thanks in advance
There’s no need to download external libraries – everything you need for this is already built into PHP.
Steps:
You can use
SELECT * FROM table WHERE id IN (1,2,...)mysql_fetch_array()ormysql_fetch_assoc()to get the rows one at a timefputcsv()to output them to a file ending in csv – this will properly escape your datahttp://www.php.net/manual/en/function.mysql-fetch-array.php
http://php.net/manual/en/function.fputcsv.php
Excel will be able to read the file.
Override the defaults for
fputcsvto use tabs for delimiters and Excel will have an even easier time reading the file. If you use commas (the default) you may need to pick commas as the delimiter on Excel import.Here’s a working example assuming you already have rows set up: