Skip to content

Instantly share code, notes, and snippets.

@mklooss
Created October 22, 2012 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mklooss/3931748 to your computer and use it in GitHub Desktop.
Save mklooss/3931748 to your computer and use it in GitHub Desktop.
Magento MySQL Plain Attribute Option Import
<?php
$attribute_code = "attr_code";
$suffix = " cm";
$array = array(
"10","20","30"
);
/*do not change*/
$xml = simplexml_load_file("../app/etc/local.xml");
$db = $xml->global->resources->default_setup->connection;
$db_prefix = (string) $xml->global->resources->db->table_prefix;
$username = (string)$db->username;
$password = (string)$db->password;
$dbname = (string)$db->dbname;
$host = (string)$db->host;
$sql = mysql_connect($host, $username, $password);
mysql_set_charset("utf8",$sql);
mysql_select_db($dbname,$sql);
$res = mysql_query("SELECT `{$db_prefix}attribute_id` FROM `eav_attribute` WHERE `attribute_code` = '{$attribute_code}' LIMIT 1;");
$m = mysql_fetch_array($res);
if(empty($m['attribute_id'])) {
die("no id");
}
$attrId = $m['attribute_id'];
$i=0;
foreach($array as $_row) {
$i++;
mysql_query("INSERT INTO `{$db_prefix}eav_attribute_option` (`option_id`, `attribute_id`, `sort_order`) VALUES (null,'{$attrId}','{$i}')",$sql) or die("an error 1");
$id = mysql_insert_id($sql) or die("an error 2");
mysql_query("INSERT INTO `{$db_prefix}eav_attribute_option_value` (`value_id`, `option_id`, `store_id`, `value`) VALUES (null,'{$id}', '0', '{$_row}{$suffix}')",$sql) or die("an error 3");
}
echo "done";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment