Jump to content

Recommended Posts

Hi i'm having problem that i'm sure is very easy but i don't know the answer. I'm trying to create a csv file with php but some of the fields i'm creating contain commas already. I thought i had to add "$data", like that with the speech marks round it. But whatever i do it's not opening great in excel.

 

My code is below:

$q = "select * from main_site_languages limit 1";

$r = mysql_query($q) or die (mysql_error()); 
$headings = mysql_fetch_assoc($r);

$res = mysql_query("select * from main_site_languages");

$csv = "";

foreach ($headings as $col => $v)
{
	$csv .= strtoupper($col).",";
	$h[] = $col;
}

$csv = substr_replace($csv,"", -1);
$csv .= "\n";

while ($row = mysql_fetch_array($res, MYSQL_ASSOC))
{
	$csv .= '"'.$row[$h[0]].'","'.$row[$h[1]].'","'.$row[$h[2]].'","'.$row[$h[3]].'","'.$row[$h[4]].'","'.$row[$h[5]].'","'.$row[$h[6]].'","'
				.$row[$h[7]].'","'.$row[$h[8]].'","'.$row[$h[9]].'","'.$row[$h[10]].'","'.$row[$h[11]].'","'.$row[$h[12]].'","'.$row[$h[13]].'","'
				.$row[$h[14]].'","'.$row[$h[15]].'","'.$row[$h[16]].'","'.$row[$h[17]].'","'.$row[$h[18]].'","'.$row[$h[19]].'","'.$row[$h[20]].'","'
				.$row[$h[21]].'","'.$row[$h[22]]."\n";
}

header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="main_site_languages_' . date("Y-m-d") . '.csv"');
  	print $csv;
    exit;

 

any help would be great I'm racking my brains!!  :)

no worries this is just one line from the database:

(4, '[pro]To remove an item just click the ''REMOVE'' button. <br />This will remove one item only.', '[amt]Pour supprimer un article, cliquez sur le "Supprimer" bouton.<br /> Cela permettra d''éliminer un point seulement.', '[amt]So entfernen Sie einen Artikel klicken Sie einfach auf den "Entfernen"-Taste. <br />Dadurch wird nur ein Element.', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'show_cart.php', 'no'),

 

You can see the 3rd record has a comma in the field. Each $row is one of these fields.

 

Thanks again

Premiso -that's a great idea i now feel like an idiot for not even thinking of that!! Cheers.

 

Seems they add double quotes round the data and if there's already a double quote in the data they quote that too ie "" so i think i'll use a str_replace on it.

 

Cheers

Temporarily comment out the two header() statements so that you can see what $csv contains. At a minimum, you are missing a leading " on the header line that will throw everything off. You are also missing a " at the end of each data line, before the \n.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.