?
Solved

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

Posted on 2014-10-16
5
Medium Priority
?
58 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 2000 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 111

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 111

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

Industry Leaders: 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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.
Suggested Courses

770 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