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
Solved

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

Posted on 2014-10-16
5
55 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
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.

856 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