/ Published in: PHP
The best part about this script is that any table you put in the query on line 14 will be the headers and data in the CSV. No need to hand type out all the table headers unless you want to comment that section out and put in your own. So if you have a table full of users that includes: name, email, website, phone you could create a query like:
SELECT name, email, website, phone FROM users
This would pull only those fields to the CSV if you had other data like passwords, nicknames or instant messenger addresses in the same table.
SELECT name, email, website, phone FROM users
This would pull only those fields to the CSV if you had other data like passwords, nicknames or instant messenger addresses in the same table.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php function parseCSVComments($comments) { return '"'.$comments.'"'; // If I have new lines or commas escape them } else { return $comments; // If no new lines or commas just return the value } } if($numberFields) { // Check if we need to output anything for($i=0; $i<$numberFields; $i++) { $head[] = mysql_field_name($sql, $i); // Create the headers for each column, this is the field name in the database } $data = ""; foreach($head as $fieldName) { // Loop through the array of headers as we fetch the data $row[] = parseCSVComments($info->$fieldName); } // End loop $row = ''; // Clear the contents of the $row variable to start a new row } // Start our output of the CSV echo $headers.$data; } else { // Nothing needed to be output. Put an error message here or something. echo 'No data available for this CSV.'; } ?>
URL: http://www.wlscripting.com/tutorial/37