473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

CSV export thing

Hi all

I am looking at the fputcsv function for something that I need. In summary,
this is what I want to do:

1. Go through a table
2. Get all the columns and put them into an array
3. write the array to a CSV file
4. Go to the next row
5. Append the table row into the CSV file
6. Repeat for each row in the table

The php site shows this:

<?php

$list = array (
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"'
);

$fp = fopen('file.csv', 'w');

foreach ($list as $line) {
fputcsv($fp, split(',', $line));
}

fclose($fp);
?>

I'm assuming that I need the 'a+' mode for opening the file and not 'w'?

I'm ashamed to say that I have no idea how to loop through the table and put
the columns into the array.

What I'm coming up with at the moment is this:

<?php do {

$list = array (
'
$row_Recordset1['firstname'],
$row_Recordset1['lastname'],
$row_Recordset1['email'],
$row_Recordset1['telephone']'
'
);

$fp = fopen('/path/to/the/file.csv', 'a+');

foreach ($list as $line) {
fputcsv($fp, split(',', $line));
}

fclose($fp);

} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

Probably very inelegant!

Does it look right though?

Andy

Jul 17 '05 #1
2 2004
I noticed that Message-ID: <BE****************@redcatmedia.net> from
Andy Jacobs contained the following:
Probably very inelegant!

Does it look right though?


Looks a bit long winded unless I'm missing summat.

How about something like:

//if not appending to existing file
$csv= "firstname,lastname,email,telephone\n";
//otherwise
$csv="";

while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)){
$csv.=implode(",", $row_Recordset1)."\n";
}

Then just write $csv to a file.

Untested, obviously.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2
DH
Andy Jacobs wrote:
Hi all

I am looking at the fputcsv function for something that I need. In summary,
this is what I want to do:

1. Go through a table
2. Get all the columns and put them into an array
3. write the array to a CSV file
4. Go to the next row
5. Append the table row into the CSV file
6. Repeat for each row in the table


The following was adapted from
http://www.phpfreaks.com/tutorials/114/0.php
$header = '';

$data = '';

$export = @mysql_query("SELECT * FROM $tb") or die(mysql_error());

$column_count = @mysql_num_fields($export) or die(mysql_error());

for($i = 0; $i < $column_count; $i++){
$header .= @mysql_field_name($export, $i)."\t";
}

while($row = @mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == '')) {
$value = "\t";
}else{
$value = str_replace('"', '""', $value);
$value = '"'.$value.'"'. "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}

$data = str_replace("\r", '', $data);

if($data == ''){
$data = "\n(0) Records Found!\n";
}

@header("Content-type: application/octet-stream");
@header("Content-Disposition: attachment; filename=$tb.xls");
@header("Pragma: no-cache");
@header("Expires: 0");
print "$header\n$data";
mysql_close();
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
6
by: Robin Cushman | last post by:
Hi all, I need some help -- I'm working with an A2K database, using DAO, and am trying to read records into a Crystal Report and then export it to a folder on our network as an Excel...
2
by: GAVO-UK | last post by:
Using Ms Access 2003 I am trying to export a query to a delimited txt file containing some CURRENCY fields, which I want to export without the currently symbol and with five decimals. I have...
2
by: Regnab | last post by:
I've got my code working so that it'll count the number of columns in the table and move across (eg Range A-P and then range Q-W). Problem is when I get to the end of the single letters and get...
3
by: Marja Ribbers-de Vroed | last post by:
Hi, One of my clients has a strange problem with my webapplication which I think may be related to some firewall setting on his computer. On several screens in the application, it is possible...
3
by: Iavor Raytchev | last post by:
Hello, We a situation with a central database that contains the data that needs to be presented at N off-line terminals (N can be 5 000 can be 15 000). Each terminal presents unique data. The...
6
by: bsma1 | last post by:
I building a web service that has an enum I want the consuming application to be able to use. I have the enum declared in the web service as: public enum myEnum { ONE = 1, TWO = 2, };
3
by: ll | last post by:
Currently, I'm exporting a database from production to local (test) machine. I've done this several times without problem, but during the last few days, the primary keys have failed to export. ...
8
by: iheartvba | last post by:
Hi I am using Access 2007 and am trying to export a query to a fixed length text file. I tried using the following code to export the text file: DoCmd.TransferText acExportFixed, , "qryFFRDeFile",...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.