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);
Related
Hello Every one,
I have an issue that is, I want to add an integer into a string for example I have two text field one is called series start and the other one is called series end now if user enters
FAHD1000001 into series start field
AND
FAHD1000100 into series end field
the algorithm should store 100 values into the database with increment into the each entry that is going to store into the database. i.e
FAHD1000001, FAHD1000002, FAHD1000003, ........., FAHD1000100
Is it possible to do so, if yes then how. Please help me
You have to do something like this but there is a loop hole if you have any numeric value in name like F1AHD00001 than it will not work
$str1 = $request['start'];
$str2 = $request['end'];
$startint=preg_replace("/[^0-9]/","",$str1);
$endint=preg_replace("/[^0-9]/","",$str2 );
$words = preg_replace('/[[:digit:]]/', '', $str1);
for($i=$startint;$i<$endint;$i++){
$newstring=$words.$i;
//Save this new String
}
I have a select menu for users.
It is populated by PHP variables, which are all language variables. For example:
$word = $lang['word'];
$select = array($word);
Therefore, the select menu options will change based on the language the user has chosen. I need to be able to compare users' selections to each other. For example:
if($user1word == $user2word) ...
But because of the language files, this doesn't work. Obviously "one" != "Uno" even though they're the same.
My first fix was to change everything to a numeric value before posting it to the database. Example:
if($_POST['word'] == $lang['word']) { $userWord = 1 }
This worked perfectly for all words except those that contained special characters (å, æ, é...) and nothing I did could resolve this (I tried normalizer; language-specific accept-char onchange events for the form; utf8_encode. It was hopeless.
Currently everything saves to the database as text, dependent on the language the user is in. So if "Language" is an option, but you're in Norwegian, it saves as "Språk".
I need a simple solution that doesn't crush my mind - I am new to PHP.
Currently everything saves to the database as text, dependent on the language the user is in.
This is a design flaw in my opinion. Ideally your data would be as agnostic as possible to language and translations would be performed just for the UI with tools like gettext. Typically items like select values would be stored with keys or IDs.
Use a map that is based on a common index, this index can be english, spanish or numeric. i'd suggest numeric. Store the numeric index in your database.
A step in the right direction:
$lang['en'][0] = 'Hello';
$lang['de'][0] = 'Hallo';
$lang['es'][0] = 'Hola';
$lang['en'][1] = 'Sup?';
$lang['de'][1] = 'Wie gehts?';
$lang['es'][1] = 'Que pasa?';
$userLang = 'en';
// show the select
echo '<select name="word">';
foreach ( $lang[$userLang] as $index => $word {
echo '<option value="'.$index.'">'.$word.'</option>';
}
echo '</select>';
// show the selected word:
echo 'You chose to say "'.$lang[$userLang][$_POST['word']].'".';
// compare the word to what is in the db
if ( $_POST['word'] === $dbRow['word'] ) {
// expression matches!
// assume a column in the db "language" describes the language the user chose, e.g. 'en', 'de', or 'es'
echo 'You previously chose "'.$lang[$dbRow['language']][$dbRow['word']].'" in the language "'.$dbRow['language'].'".;
}
Depending on how you want to organize it, you may favor grouping by phrase instead of grouping by language, i.e.:
$lang[0]['en'] = 'Hello';
$lang[0]['de'] = 'Hallo';
$lang[0]['es'] = 'Hola';
Depending on the current page, I would like to change the css of the chosen menu module and make others different, etc. All that while building dynamically the page before loading.
My problem is that I have a serie of variables going by this structure :
$css_(NAME_OF_MODULE)
To know what variable must be set , I have another variable I received in parameters of this functions, called
$chosen_menu
Say $chosen_Menu = "C1", I would like to add content to $css_C1. Thus, what I want is to create a variable name out of 2 variables, named $css_C1
I have tried :
${$css_.$chosen_menu} = "value";
But it doesnt seem to work. Any clue ?
That probably won't just work. PHP supports full indirection though, so something like this will work.
$varName = $css."_".$chosen_menu;
$$varName = "value";
If not, it will probably be attempting to interpret $css_ as a variable name in your second code sample, so just change that to $css."_".$chosen_menu.
$nr = 1;
${'name' . $nr} = 20 ;
var_dump($name1);
Check out http://php.net/manual/en/language.variables.php
You should be able to use:
$menu = $css . '_' .$chosen_menu;
$$menu = 'some value';
or
${$menu} = 'some value';
I'm not sure if I get you right, but how about this:
${$css."_".$chosen_menu} = "value";
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");
I'm trying to populate a template with variables from a database. The data looks as follows:
id field content
1 title New Website
1 heading Welcome!
1 intro This is a new website I have made, feel free to have a look around
2 title About
2 heading Read all about it!
What I need to do with that data is set properties of a $template object according to the field column and set said values to what's in the content column; e.g. for id = 1
$template->title = 'New Website';
$template->heading = 'Welcome!';
$template->intro = 'This is a new websi...';
I have the data in an array and I can easily loop over it but I just can't figure out how to get the properties to be the names of another variable. I've tried the variable variables approach but to no avail. Does that work with object properties?
Here's what I've got so far:
foreach($data as $field)
{
$field_name = $field['field'];
$template->$$field_name = $field['content'];
}
I've also tried using $template->${$field_name} and $template->$$field_name but so far no luck!
$template->{$field_name}
You're getting ahead of yourself.
try:
$template->$field_name;
Why does this work?
$foo = 'bar';
$object->bar = 1;
if ($object->$foo == $object->bar){
echo "Wow!";
}
use
$template->{$field_name} = $field['content'];
or simply
$template->$field_name = $field['content'];