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 8700385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:09:29+00:00 2026-06-13T02:09:29+00:00

I am writing a script to create Magento attributes programatically, pulling the data from

  • 0

I am writing a script to create Magento attributes programatically, pulling the data from a CSV. Not sure I have the actual loop correct that pulls the data from the CSV – was hoping for some expert guidance on the logic?

    <?php 
$fh = fopen("attributes.csv", "r");
$i = 0;
while (($l = fgetcsv($fh, 1024, ",")) !== FALSE) {

    $i++;
    if($i == 1) continue; //ignoring the headers, so skip row 0
    $data['label'] = trim($l[2]);
    $data['input'] = trim($l[3]);
    $data['type'] = trim($l[2]);

    //Create the attribute

    $data=array(
'type'=>$data['type'],
'input'=>'text',
'label'=>$data['label'],
'global'=>Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'is_required'=>'0',
'is_comparable'=>'0',
'is_searchable'=>'0',
'is_unique'=>'1',
'is_configurable'=>'1',
'use_defined'=>'1'
);

$model->addAttribute('catalog_product','test_attribute',$data);

}   

?>

I basically just want it to grab the attribute data from the CSV, and for each row in the CSV run the code to create it (using the label and name as specified in the CSV – im guessing I am missing something obvious in the loop? (just really learning what I’m doing!)

  • 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-13T02:09:30+00:00Added an answer on June 13, 2026 at 2:09 am

    You reset the $data array in each loop, after inserting the values from CSV, so the CSV-content gets lost. Try this

    $fh = fopen("attributes.csv", "r");
    $i = 0;
    $attributes=array(); //!!
    while (($l = fgetcsv($fh, 1024, ",")) !== FALSE) {
    
        $i++;
        if($i == 1) continue; //ignoring the headers, so skip row 0
    
        $data=array();
    
        $data['label'] = trim($l[2]);
        $data['input'] = trim($l[3]);
        $data['type'] = trim($l[2]);
    
        $data['global']=Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL;
        $data['is_required']='0';
        $data['is_comparable']='0';
        $data['is_searchable']='0';
        $data['is_unique']='1';
        $data['is_configurable']='1';
        $data['use_defined']='1';
    
        //insert $data to the attributes array
        $attributes[]=$data;
        //or
        $model->addAttribute('catalog_product','test_attribute',$data);
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a script and I need to create a loop that will execute
I am writing a Perl script and using the Class::CSV module. I have an
im writing a script to get services from local and remote machines. I've had
I am currently writing a script which parses the ServiceTage, Computername and Username from
I'm writing a PHP script to generate SQL dumps from my database for version
I'm using SQL Server MS. I'm having trouble writing this script: CREATE VIEW rental_view
I'm writing a script to create a bunch of tables, and I read in
Writing a script to automatically create an appointment in outlook via javascript. It works
I am currently writing a script that lets me import multiple products in magento.
I am writing a PHP script where the inputs are: From date To date

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.