473,574 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,dd dd',
'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_Recordset 1 = mysql_fetch_ass oc($Recordset1) ); ?>

Probably very inelegant!

Does it look right though?

Andy

Jul 17 '05 #1
2 2013
I noticed that Message-ID: <BE************ ****@redcatmedi a.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,last name,email,tele phone\n";
//otherwise
$csv="";

while ($row_Recordset 1 = mysql_fetch_ass oc($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("S ELECT * FROM $tb") or die(mysql_error ());

$column_count = @mysql_num_fiel ds($export) or die(mysql_error ());

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

while($row = @mysql_fetch_ro w($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("Conten t-type: application/octet-stream");
@header("Conten t-Disposition: attachment; filename=$tb.xl s");
@header("Pragma : no-cache");
@header("Expire s: 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
10496
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
13119
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 spreadsheet. I'm having trouble with my code at the point at which it hits ".ReadRecords" -- the module just runs and runs without generating anything. I've...
2
2560
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 tried everything to get the currency fields with the format I want after exporting but!! NO LUCK!!! This is what I have tried. In the source table...
2
3465
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 into AA etc. Have got an idea of how I could do it by using Chr() and having an ongoing counter which is divided by 26 when it goes past Z to work out...
3
2008
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 to click on an icon to start an export of data to Excel. The click on the icon triggers a form submit (a post, not a get) to a blank page, thereby...
3
3987
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 central database is used for data preparation. Then the data for each terminal is exported as separate SQL file. The terminals run the same application...
6
10041
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
3568
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. Would anyone know what options might keep the keys from exporting? Thanks, Louis
8
18761
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", "C:\qryFFRDeFile.txt", True Access asks for a specification name, now here lies the challenge, how do I create an export specification I have...
0
7742
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8089
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8260
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7840
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6493
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5638
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5329
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3765
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2257
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.