Magento provides a functionality to directly export the content from the grids in admin section like Sales, Products etc in the form of CSV or XML. But sometimes you need to generate a CSV with the custom data. Magento has already a nice class for it, it can be found at lib/Varien/File file name Csv.php. This class can be used to read and write CSV.
Also, read:
- Display Related products on product details page in Magento
- Magento – Models, resource models, and collections
- Know About Magento Object Relational Mapping (ORM)
- Creating Custom Magento URL Rewrites
This example code will save the products data into the CSV file.
<?php
$file_path = "/your_dir_path/sample.csv";
//file path of the CSV file in which the data to be saved
$mage_csv = new Varien_File_Csv(); //mage CSV
$products_ids = array(1,2,3); //product ids whose data to be written
$products_model = Mage::getModel('catalog/product'); //get products model
$products_row = array();
foreach ($products_ids as $pid)
{
$prod = $products_model->load($pid);
$data = array();
$data[‘sku’] = $prod->getSku();
$data[‘name’] = $prod->getName();
$data[‘price’] = $prod->getPrice();
$products_row[] = $data;
}
//write to csv file
$mage_csv->saveData($file_path, $products_row);
You may also like:
- Working with php.ini file Configuration
- Control Statements in PHP
- Convert Associative Array into XML in PHP
- Convert XML into Associative Array in PHP
- Using Prepared Statement with PHP & MySQL
- How to Upload File in PHP
- Converting an Array to JSON in PHP
- Converting JSON to Array or Object in PHP
- Manipulating PHP arrays: push, pop, shift, unshift
- Remove Repeated Words From String in PHP
- Converting a PHP Array to a Query String
- 15+ regular expressions for PHP developers
- 10 Most Important Directory Functions in PHP
- 10 little known but useful PHP functions
- PHP Script to Download Large Files Reliably
I want to upload 100 no’s of products so how would i know that all data is saved.
You need to make changes in your script to add check for the number you want to display save message in foreach loop.