Solved

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

Posted on 2014-10-16
5
50 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
Comment Utility
Why not just remove them from the $output line 16 and 25?
0
 

Author Comment

by:brucegust
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now