Solved

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

Posted on 2014-10-16
5
56 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 110

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

733 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