I've got the following problem. In an extra module, I want to sort the options of an attribute, based on their position. When I try to get the Option of an Attribute, I can get the Id and the Label, but there is nothing mor ein that object.
I can do, for example, this:
$attribute = $_product->getResource()->getAttribute($code)->getOptionsText($value):
Or just getOptionId($value), but there is nothing to get the Position, which is editable in the backend. So, how to get this? Havn't found anything (useful) on the net yet.
(Also the similar question magento sort attribute option collection by position? doesnt give any help)
EDIT:
What I managed to do, is doing a direct SQL statement, like this:
SELECT sort_order FROM mag_eav_attribute_option WHERE option_id = 114 AND attribute_id = 533;
But I think, there is a better option to get that value.
I found the answer myself and want to share it with you.
$attribute = Mage::getModel('eav/entity_attribute')->load( $code, 'attribute_code');
$option_col = Mage::getResourceModel( 'eav/entity_attribute_option_collection')
->setAttributeFilter( $attribute->getId() )
->setStoreFilter()
->setPositionOrder( 'ASC' );
$option_col->getSelect()->order('main_table.sort_order '.$orderby);
I hope it helps someone.
This is actually quite simple if you take a look # Mage_Eav_Block_Adminhtml_Attribute_Edit_Options_Abstract
$attributeId = 230;
$options = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter($attributeId)
->setPositionOrder('desc', true)
->load();
foreach($options as $opt){
Mage::log($opt->getSortOrder());
}
I see you came up with something similar already but I thought I would post this as it may be helpful to others.
Start to debug then:
print_r($_product->getResource()->getAttribute($code));
print_r($_product->getResource()->getAttribute($code)->getData());
print_r(get_class_methods($_product->getResource()->getAttribute($code)));
Okay, so you have your attribute code $code and your attribute option value $value. Then you can get the corresponding sort order like that:
$source = $product->getResource()->getAttribute($code)->getSource();
$optionId = $source->getOptionId($product->getData($code));
$optionSortOrder = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setIdFilter($_optionId)
->getFirstItem()
->getSortOrder();
From: http://www.phptechi.com/getting-product-attributes-values-and-labels.html
How to fetch attribute value sort order?
Here is the Quick and Easy way using SQL to get attribute value sort positing.
Change value for variables in following code:
$CurtAtr is current attribute like color, size, etc
$attrVal is attribute value e.g. size has "small, medium, Large, etc"
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('catalog_read');
$read->fetchAll("SELECT ao.sort_order FROM eav_attribute_option_value as aov, eav_attribute_option as ao where value='$attrVal' and aov.option_id = ao.option_id and attribute_id=$CurtAtr limit 1");
Related
in my data base i have a table "options" which contains options from my site like title, language etc.
i want to load all this options and store it in a array query the database.
this code works manual
$option=array("title" => "Site's title", "option2" => "option 2 value");
echo $option[title];
but when i make the query...
$query_options=mysql_query("SELECT * FROM options");
while($data_options = mysql_fetch_row($query_options)){
$option=array($data_options[1] => $data_options[2]);
}
echo $option[title];
it doesn't work.
Hope you can help me.
Thank you
mysql_fetch_row() only allows you to access columns in your query based on the column ordering : $options[0] or $options[1].
Use mysql_fetch_array() instead. With it you can access data with $options["title"] for example. Using the column name.
See the doc at http://www.php.net/manual/fr/function.mysql-fetch-array.php
PS - Don't forget to put title between quotes too!
$query_options=mysql_query("SELECT * FROM options");
$array = array();
while($data_options = mysql_fetch_array($query_options)){
$array[]["title"] = $data_options['title'];
$array[]["value"] = $data_options['value'];
}
var_dump($array);
Should work. lemme know how it goes.
Also, you should know that you shouldn't use any mysql_* commands, they're becoming depreciated as they're in-secure, you should take a look here
http://www.php.net/manual/en/ref.mysql.php
and
http://www.php.net/manual/en/ref.mysqli.php
I'm trying to get all of the notes from a particular evernote notebook. I am able to display all of the data as an array, and I'm trying to use a foreach loop to get the title. I also want to be able to get the content, date, etc.
$filter = new NoteFilter();
$filter->notebookGuid = $notebookGuid;
$notelist = $client->getNoteStore()->findNotes($authToken, $filter, 0, 100);
foreach($notelist as $value) {
echo $value->title;
}
I know that I'm being really stupid, but I'm new to php and evernote. Any help is appreciated!
The return value of NoteStore.findNotes is NoteList which is not a collection. You have to get notes attribute from NoteList and then iterate it.
By the way, findNotes is now deprecated so please use findNotesMetadata.
You might want to check the following example from evernote:
https://github.com/evernote/evernote-sdk-php/blob/master/sample/client/EDAMTest.php
I'm having an issue producing this array multiple times upon how many coupons purchased.
Now it looks like
$coupon_array = array(
'user_id'=>$_POST["user_id"],
'mergent_id'=>$_POST["merchant_id"],
'deals_id'=>$_POST["deal_id"],
'order_id'=>$order_id,
'secret'=>$secret,
'expire_time'=>$time,
'create_time'=>$time,
'status'=>1
);
$this->common_model->insertData('coupon', $coupon_array);
But i have a post value such as:
"quantity"=>$_POST["quantity"]
and i would like to produce this X times. Example:
$quantity x $this->common_model->insertData('coupon', $coupon_array);
Sorry for my english, and i hope i explain this so it's understandable... ;)
Another one! when we insert the coupons they all have the same md5($secret), is it possible to have that also with all the different code...
$secret = md5($secret);
$coupon_array = array(
'user_id'=>$_POST["user_id"],
'mergent_id'=>$_POST["merchant_id"],
'deals_id'=>$_POST["deal_id"],
'order_id'=>$order_id,
'secret'=>$secret,
'expire_time'=>$time,
'create_time'=>$time,
'status'=>1
);
Well, if I understand what you want, you can use for, but that's obvious:
for($i=0; $i<$this->input->post('quantity');$i++) {
$coupon_array['secret'] = md5($coupon_array['secret'].$i);
$this->common_model->insertData('coupon', $coupon_array);
}
Also, never use $_POST["..."] in CodeIgniter, use only $this->input->post('...') as it escapes properly. More info about input class can be found here.
for ($i=0; $i<$quanity; $i++) {
$this->common_model->insertData('coupon', $coupon_array);
}
I know it might be a silly question to ask, but I have a field say a and b, now how to get the value and set the value for a and b.
Right now my code is like this..
$n = node_load($node->id);
$n->title;
I am getting the node title, I want to know how to get and set the value for a and b please, and if i set the value of a and b will itt be saved using
node_save($n);
??
It depends a bit which version you're using and on the particular field types you're using, but something like this:
// Drupal 6
$n = node_load($node->id);
$n->title = 'A title';
$n->field_my_field_a[0]['value'] = 'A value';
$n->field_my_field_b[0]['value'] = 'B value';
node_save($n);
// Drupal 7
$n = node_load($node->id);
$n->title = 'A title';
$n->field_my_field_a[LANGUAGE_NONE][0]['value'] = 'A value';
$n->field_my_field_b[LANGUAGE_NONE][0]['value'] = 'B value';
node_save($n);
In both cases the field data will be saved along with the node when you call node_save().
It's worth noting that the 0 index in both cases refers to the first item in a field. If a field has multiple values you can just keep adding to the array. The value key might need to change depending on the type of data that the field holds (for example a filefield will hold the fid (file id) of the file it holds so adjust accordingly.
Also LANGUAGE_NONE might need to be replaced by the required language code if you're using the Drupal 7 version.
Your question is a little confusing because you never explain what a and be are. But to access a cck field generally looks like this:
$node = node_load($nid);
$field_value = $node->field_name[0]['value'];
If it's a multiple select have values in offsets past zero. You can set the value using that same method:
$node = node_load($nid);
$node->field_name[0]['value'] = $field_value;
node_save($node);
In some part of my code I need something like this:
$product_type = $product->type;
$price_field = 'field_'.$product_type.'_price';
$price = $product->$$price_field;
In other words I need kind of KVC - means get object field by field name produced at the runtime.
I simply need to extend some existing system and keep field naming convention so do not advice me to change field names instead.
I know something like this works for arrays, when you could easily do that by:
$price = $product[$price_field_key].
So I can produce key for array dynamically.
But how to do that for objects?
Please help me as google gives me river of results for arrays, etc...
Thank you
$price = $product->{$price_field};
Sorry Guys.
It was much easier than I thought.
Hopefullty it will help someone. Simply put:
$price_field = 'field_'.$product_type.'_price';
$price = $product->$price_field;
So you can use varialbe to get object field in Php.
I went to far with those $$ ;-)
Regards
How about using get_object_vars?
$price_field = 'field_'.$product_type.'_price';
$instvars = get_object_vars($product);
$price = $instvars[$price_field];
Actualy it would work as follows.
$product_type = $product->type;
$price_field = "field_".$product_type"._price";
$price = $product->$price_field;