Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:52:17+00:00 2026-06-17T16:52:17+00:00

The following code is supposed to output 4 fields into a CSV, Category, SKU,

  • 0

The following code is supposed to output 4 fields into a CSV, Category, SKU, Price and Special Price, and for the most part works well except the output has 3 quote marks around all items like thus

"""Joystick Head""","""Man222""","""155""","""124.95"""

yet the output in the browser is fine

Type: "Joystick Head" SKU: "Man222" Price: "155" Special Price: "124.95"

I have tried str_replace, implode and anything else i can think of, with varying results, sometimes managing to remove almost all quotes.

I am hoping this is something silly and I have been staring at it too long, help?

define('SAVE_FEED_LOCATION','var/export/prices.csv');
set_time_limit(0);
require_once 'app/Mage.php';
Mage::app('default');

try{
    $handle = fopen(SAVE_FEED_LOCATION, 'w'); 

    $attributeName = 'producttype';

    $products = Mage::getModel('catalog/product')->getCollection();
    $products->addAttributeToFilter('status', 1);
    $products->addAttributeToFilter('visibility', 4);
    $products->addAttributeToSelect('*');
    $prodIds = $products->getAllIds();

    $product = Mage::getModel('catalog/product');

    $counter_test = 0;

    foreach($prodIds as $productId) {

        if (++$counter_test > 0 && $counter_test < 100){

            $product->load($productId);
            $product_data = array();    
            $productId = $product->getId();

            $productAtt = Mage::getModel('catalog/product')->load($productId);
            $attributes = $productAtt->getAttributes();

            $attributeValue = null;     
            if(array_key_exists($attributeName , $attributes)){
                $attributesobj = $attributes["{$attributeName}"];
                $attributeValue = $attributesobj->getFrontend()->getValue($productAtt);
            }
            if ($attributeValue == "No")
            {
                $attributeValue = NULL;
            }

            $product_data['producttype'] = $attributeValue;
            $product_data['sku'] = $product->getSku();  
            $product_data['price'] = round($product->getPrice(),2);
            $product_data['special_price'] = round($product->getSpecialprice(),2);  

            // if no special price use normal price
            if ($product_data['special_price'] == NULL) { 
                $product_data['special_price'] = round($product->getPrice(),2);
            }

            foreach($product_data as $k=>$val){
                $bad=array('"',"\r\n","\n","\r","\t");
                $good=array(""," "," "," ","");
                $product_data[$k] = '"'.str_replace($bad,$good,$val).'"'; 
            }

            echo "Type: ". $product_data['producttype'] . " SKU: " . $product_data['sku'] . " Price: ". $product_data['price'] . " Special Price: " . $product_data['special_price'] . "<br>";

            fputcsv($handle, $product_data, ',', '"');
        }
    }
    fclose($handle);
    echo '<br> Completed at: ' . time();
}
catch(Exception $e){
    $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
    error_log($e->getMessage(), 1, 'myemail@mydomain.com', $headers);
    die($e->getMessage());
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:52:18+00:00Added an answer on June 17, 2026 at 4:52 pm

    The issue is in line

    $product_data[$k] = '"'.str_replace($bad,$good,$val).'"';
    

    There the code adds double quotes to each value of the array. Later the fputcsv() escapes quotes by tripling them to properly output data in CVS format. You can take a look at examples in PHP documentation for fputcsv() to see, that this is a normal behavior.

    There is nothing wrong in 3x double quotes, as it is just a method to preserve double quotes in original data, while passing values to another system via CSV transport. However, if you intentionally strip double quotes, then that line looks like having a typo. It is expected to be:

    $product_data[$k] = str_replace($bad, $good, $val);
    

    Additional notes

    1. You don’t need to strip double quotes from the values. It is not a
      bad symbol for CSV. I suggest to remove double quote from bad symbols and do not mind triple double quotes at all.
    2. The str_replace() can take array as third
      argument. Threfore you can replace all bad values in one call, rather then doing it in foreach cycle. E.g.: $product_data = str_replace($bad, $good, $product_data);
    3. You don’t need to redeclare bad and good value arrays on each
      cycle iteration. They are not changed in the script, so their declaration can be
      moved right before foreach ($prodIds as $productId) { line
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code gives an error when it's supposed to output just std::endl :
The following code is supposed to output the contents of a database field to
The following code generates an inconsistent output. It is supposed to log testString is
The following code is supposed to find electors that do not have a postal
The following code is supposed to make the right 60% of the display red.
I am creating a SMS app the following code is supposed to: check if
Given the following code (it's supposed to write helloworld in a helloworld file, and
The following code is supposed to take a string that may or may not
I have the following code that is supposed to display 3 products when the
The following code does not produce the expected output. Why? wchar_t* wchar_t_printf_return(wchar_t* formatstring, ...){

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.