Solved

I need to tweak this code that produces a csv file - Part III

Posted on 2014-10-16
5
54 Views
Last Modified: 2014-10-20
Here's the code that I'm using to produce a CSV file:

$michelle="select actor_id, actor_display_name, posted_time, geo_coords_lat, geo_coords_lon, location_name from twitter_csv_test where geo_coords_lat BETWEEN '$lat_1' AND '$lat_2' and geo_coords_lon BETWEEN '$lon_2' and '$lon_1'";
$michelle_query=mysqli_query($cxn, $michelle);
if(!$michelle_query)
{
$rats=mysqli_errno($cxn).': '.mysqli_error($cxn);
die($rats);
}
$michelle_columns=mysqli_field_count($cxn);

//gets the field names from your table and sets them up as the first row in your csv file

$heading=mysqli_fetch_fields($michelle_query);

foreach ($heading as $val)
{
	$output .='"'.$val->name .'",';
}
$output .="\n";


while($michelle_row=mysqli_fetch_array($michelle_query))
{
	for($i=0; $i<$michelle_columns; $i++)
	{
		$output .='"'.$michelle_row["$i"].'",';
	}
$output .="\n";
}

$filename="twitter.csv";
header('Content-type:application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;

Open in new window


It works fine, but my end user needs to have all of the double quotes removed. Reason being is that the data isn't being viewed as a spreadsheet, rather it's being imported into another system.

So how can I insert my data from my database into this csv file sans the double quotes?
screenshot.docx
0
Comment
Question by:brucegust
  • 2
  • 2
5 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40385364
Why not just remove them from the $output line 16 and 25?
0
 

Author Comment

by:brucegust
ID: 40385379
Is it that simple? I thought those quotes were a necessary part of the syntax so I didn't fool with them. I'll do it!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40385385
If you use the PHP built-in function fputcsv() you can get standard CSV output.  It will only apply quotes when they are required.  You can control the quotes and delimiters with that function.  If you write the data to an intermediate temporary working file, you can use fputcsv().

Have you asked the client about getting JSON instead of CSV?  It might be worth considering.

Sure would like to see that test data set.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40385417
Assuming there are no commas in the db fields then they are not needed, main reason for using them is where you have a delimiter that may be part of the data
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40385427
I think the CSV standard calls for quoting blanks, too -- not just the delimiters.  This is the kind of place where some test data would really help.  We could try a few things and show you the right way to do it.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question