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

PHP SOAP client that understands multi-part messages?
php check if domain equals value, then perform action
How can I implement RSS 2 in Code Igniter?
Error with MySQL syntax using REPLACE INTO
how to store chechbox value in php
mysql muliple queries in one statement
PHP Static Variables
php code analysis
PHP Object Validation
How to use cms systems like drupal and so on for crawl only? [closed]
How do I loop through MySQL results easily?
Accessing Mac applications from Ruby or PHP or Cocoa
How do I run PHP code when a user clicks on a link?
How do you just get the vars in a url using php?
ArrayCollection in PHP
Recursively build XML from PSQL Result Set (using PHP)

Categories

HOME
opengl
rust
webview
automapper
facebook-messenger
yahoo-weather-api
key
website
google-webmaster-tools
tibco
apache-kafka-connect
mosquitto
angular-meteor
lda
nexus3
documentum
amazon-elb
jodatime
mahout-recommender
corona
uima
tfsbuild
opam
activeadmin
kaggle
quantitative-finance
environment
jpa-2.1
web-config-transform
android-cardview
gravity-forms-plugin
adapter
reporting
series
overwrite
creation
spring-rabbitmq
android-canvas
landscape
ftp-server
nsuserdefaults
widestring
slideshow
yoast
section508
knockout-2.0
nivo-slider
portability
pecl
word-2016
finder
thread-exceptions
openquery
jsf-2.3
node.js-connect
gravatar
trust
ui4j
msgpack
stack-smash
self
openshift-cartridge
http.client
dnx50
asp.net-web-api-helppages
knife
hmisc
wso2ml
google-gdk
explain
mxe
debug-symbols
fat32
datagridcomboboxcolumn
rubber
ildasm
smacss
back
object-code
angular-ui-select
xcode6.3.1
nachos
commenting
xulrunner
application-blocks
illuminate-container
yii-booster
notifyjs
sly-scroller
aero
icenium
tridion-2011
jquery-dialog
towers-of-hanoi
thejit
uipangesturerecognizer
liveconnect
qt-mobility
digiflow
explicit
reliability
facebook-fbml
ppc
inversion
index.dat
watchpoint
isapi-redirect
jvm-bytecode

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App