refiking Posted September 30, 2009 Share Posted September 30, 2009 I want to write a line to a csv file and I am having trouble with my $attributes variable because it contains commas. I need to include the commas in, so how can I do that? Please keep in mind that it produces the correct result except for the attributes column. Here's the relevant code: //Example is $attributes = Color,Size,Shape,Texture $csv_data = $sku . ', ' . $category . ',' . $subcategory . ',' . $title . ',' . $description . ',' . $price . ',' . $image . ',' . $attributes; fputcsv($file2, split(',', $csv_data)); Link to comment https://forums.phpfreaks.com/topic/176084-solved-problem-with-fputcsv/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 30, 2009 Share Posted September 30, 2009 Don't build $csv_data as a string first then split it. That just doubles the work needed. Just build the array directly by putting each value into consecutive array elements. Link to comment https://forums.phpfreaks.com/topic/176084-solved-problem-with-fputcsv/#findComment-927830 Share on other sites More sharing options...
refiking Posted September 30, 2009 Author Share Posted September 30, 2009 Can you please explain that? Link to comment https://forums.phpfreaks.com/topic/176084-solved-problem-with-fputcsv/#findComment-927832 Share on other sites More sharing options...
refiking Posted September 30, 2009 Author Share Posted September 30, 2009 Here's the relevant code with all the queries... $kws = mysql_query("select products_id, products_price, products_image from products")or die(mysql_error()); $num_rows = mysql_num_rows($kws); while ($info = mysql_fetch_assoc($kws)){ $kws2 = mysql_query("select products_name, products_description from products_description where language_id = '1' and products_id = '{$info['products_id']}'"); $info2 = mysql_fetch_assoc($kws2); $sku = str_replace(',',';',$info['products_id']); $price = str_replace(',',';',$info['products_price']); $image = str_replace(',',';',$info['products_image']); $title = str_replace(',',';',$info2['products_name']); $description = str_replace(',',';',$info2['products_description']); if($attributes != ''){ $attributes = ''; } $zipimages .= '../images/' . $image; if($num_rows != ($i + 1)){ $zipimages .= ','; } $query = mysql_query("select p2c.categories_id, p2c.products_id, cd.categories_name from products_to_categories p2c, categories_description cd where cd.language_id = '1' and p2c.products_id = '{$info['products_id']}' and cd.categories_id = p2c.categories_id limit 1")or die(mysql_error()); $row = mysql_fetch_assoc($query); $category = str_replace(',',';',$row['categories_name']); $subcheck = mysql_query("select c.parent_id from categories c where c.categories_id = '{$row['categories_id']}' and c.parent_id != '0'")or die(mysql_error()); if(mysql_num_rows($subcheck) > 0){ $sub = mysql_fetch_assoc($subcheck); $sub_id = $sub['parent_id']; $subquery = mysql_query("select categories_name from categories_description where categories_id = '$sub_id' and language_id = '1'")or die(mysql_error()); $subcategory = str_replace(',',';',mysql_result($subquery, 0)); } $att_query = mysql_query("select distinct options_id from products_attributes where products_id = '$sku'"); while ($attinfo = mysql_fetch_assoc($att_query)){ $attname = mysql_query("select products_options_name from products_options where products_options_id = '{$attinfo['options_id']}' and language_id = '1' limit 1"); $attributes .= mysql_result($attname, 0) . '='; $attquery = mysql_query("select options_values_id from products_attributes where products_id = '$sku' and options_id = '{$attinfo['options_id']}'")or die(mysql_error()); $attnum = mysql_num_rows($attquery); $ai = 0; while ($values = mysql_fetch_assoc($attquery)){ $attvalues = mysql_query("select products_options_values_name from products_options_values where products_options_values_id = '{$values['options_values_id']}' and language_id = '1' limit 1"); $attributes .= mysql_result($attvalues, 0); if($attnum != ($ai + 1)){ $attributes .= ','; } else{ $attributes .= '; '; } $ai++; } } $csv_data = $sku . ', ' . $category . ',' . $subcategory . ',' . $title . ',' . $description . ',' . $price . ',' . $image . ',' . $attributes; fputcsv($file2, split(',', $csv_data)); $i++; } Link to comment https://forums.phpfreaks.com/topic/176084-solved-problem-with-fputcsv/#findComment-927836 Share on other sites More sharing options...
PFMaBiSmAd Posted September 30, 2009 Share Posted September 30, 2009 $csv_data = array($sku,$category,$subcategory,$title,$description,$price,$image,$attributes); fputcsv($file2, $csv_data); Link to comment https://forums.phpfreaks.com/topic/176084-solved-problem-with-fputcsv/#findComment-927840 Share on other sites More sharing options...
refiking Posted September 30, 2009 Author Share Posted September 30, 2009 Kudos to THE ULTIMATE GURU.... Link to comment https://forums.phpfreaks.com/topic/176084-solved-problem-with-fputcsv/#findComment-927864 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.