php


How do I export variable columns to a CSV file using PHP?


I know how to export to CSV using PHP to query mySQL and populate the CSV. However, is it possible to have the users select what columns they would like to have and vary the SQL query accordingly?
I have established the connection to the database, added my query which is:
$query = sprintf("SELECT x, y, z from a");
$result = mysql_query( $query, $conn ) or die( mysql_error( $conn ) );
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename=export.csv' );
$row = mysql_fetch_assoc( $result );
if ( $row )
{
echocsv( array_keys( $row ) );
}
while ( $row )
{
echocsv( $row );
$row = mysql_fetch_assoc( $result );
}
function echocsv( $fields )
{
$separator = '';
foreach ( $fields as $field )
{
if ( preg_match( '/\\r|\\n|,|"/', $field ) )
{
$field = '"' . str_replace( '"', '""', $field ) . '"';
}
echo $separator . $field;
$separator = ',';
}
echo "\r\n";
}
Yes it is. You have to make a form where the user selects the fields they want to export, I'm thinking in a checkbox list where you list all the posible database fields the can get, and then create the query according the user selection.
Ej:
<form action="" method="post">
<label><input name="fields[]" type="checkbox" value="name"> Username</label>
<label><input name="fields[]" type="checkbox" value="birthdate"> Birthdate</label>
</form>
And then on the server side create the database query.
$sql = "SELECT `". implode("`, `", $_POST["fields"]) ."` FROM db_table";
Hope this help.

Related Links

How to fix error after delete product from magento admin
Calling item qty from order in woocommerce pdf watermark
Laravel 5 many to many relationship
Android Studio - Fast Android Networking Library Upload File to server - error
Apostrophe in input field breaking code [duplicate]
Why do (PHP) developers store sessions in project temp folder?
PHP: How can I get the “Hello World!” out of this array in a $variable? [duplicate]
I can not use PHP's strip_tags function with another function
Cannot retrieve value from sql query
Query runs in PHPMyAdmin but not in PHP Code
Im not able to send the posted hidden value to database using code igniter
Add php variable to select options value
PHP MAIL() function sometimes not working [duplicate]
Survey in PHP not saving votes correctly to txt file
.htacess not being read. Even with AllowOverride All
is_dir and is_file not working with path from root

Categories

HOME
qt
ionic2
webview
knockout.js
android-studio
apache-nifi
swi-prolog
liferay-7
fogbugz
linker
repair
kurento
specifications
cocotb
business-intelligence
bootstrap-switch
fullcalendar-scheduler
jquery-file-upload
smartgwt
corona
nullreferenceexception
clr
pymongo
password-generator
background-color
sap-lumira
running-object-table
react-bootstrap-table
macromedia
locks
windows2012
mongodb-3.4
john-the-ripper
universal
rspec-rails
quickcheck
custom-keyboard
busboy
wdk
love2d
flowchart
suds
plane
grails3.2.0
jqgrid-asp.net
julius-speech
nomad
code-push
axis
ol3-google-maps
freshdesk
gmaps.js
hmmlearn
fitbit
decoder
nunit-console
right-to-left
xcopy
okta-api
servicestack-bsd
gdl
miniconda
arbre
green-threads
console.log
text-align
storing-data
usb-flash-drive
map
assetic
strdup
armv6
autonumber
strcmp
insertonsubmit
stty
virtual-channel
spiral
oscilloscope
multiple-dispatch
kobold2d
prototypal-inheritance
background-repeat
litespeed
qtextbrowser
filelock
getopts
airprint
j2mepolish
dm
firephp
subtext
xap
twitter-feed
levels

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App