Views: 12346
Last Modified: 23.09.2014
Examples of tasks that may occur while working with elements, sections, and infoblock properties.
Task 1:
Obtain values of all of the properties of an element knowing its ID.
1 <? $db_props = CIBlockElement::GetProperty(IBLOCK_ID, ELEMENT_ID, "sort", "asc", array());
2 $PROPS = array();
3 while($ar_props = $db_props->Fetch())
4 $PROPS[$ar_props['CODE']] = $ar_props['VALUE'];?>
Now the symbol code of the property is the key of the associative array $PROPS, I.e. if you need a value of the property with the code price, it will be stored in $PROPS['price'].
Task 2:
Obtain properties of the elements using the method CIBlockElement::GetList
1 <? $arSelect = array("ID", "NAME", "PROPERTY_prop_code_1", "PROPERTY_prop_code_2");
2 $res = CIBlockElement::GetList(array(), array(), false, array(), $arSelect);?>
Then, you have to use the cycle and obtain the properties with symbol codes prop_code_1 and prop_code_2.
Task 3:
Add a property of the type TEXT/html for an element.
If the property is not a multiple property:
01 <? $element = new CIBlockElement;
02 $PROP = array();
03 $PROP['property symbol code']['VALUE']['TYPE'] = 'text'; // or html
04 $PROP['property symbol code']['VALUE']['TEXT'] = 'property value';
05 $arLoadArray = array(
06 "IBLOCK_ID" => IBLOCK_ID,
07 "PROPERTY_VALUES"=> $PROP,
08 "NAME" => "Name of the element"
09 );
10 $element->Add($arLoadArray);?>
If the property is a multiple property:
01 <? // In $ITEMS multiple property values are stored
02 foreach($ITEMS as $item)
03 {
04 $VALUES[]['VALUE'] = array(
'TYPE' => 'text', // or html
'TEXT' => $item,
);
05 $VALUES[]['VALUE']['TEXT']= $item;
06 }
07 $element = new CIBlockElement;
08 $PROPS = array();
09 $PROPS['property symbol code'] = $VALUES;
10 $arLoadArray = array(
11 "IBLOCK_ID" => IBLOCK_ID,
12 "PROPERTY_VALUES"=> $PROPS,
13 "NAME" => "Name of the element"
14 );
15 $element->Add($arLoadArray);?>
Task 4:
Complete a multiple property of the File type. Quite often when adding an element to an infoblock several files may need to be attached to it. It can be conveniently done by creating a multiple property of the File type for the infoblock and store files there. Here is an example of the property completed:
01 <?
02 $arFiles = array();
03 for($i = 1; $i < 10; $i++)
04 {
05 if(file_exists($_SERVER['DOCUMENT_ROOT'].'/images/image_'.$i.'.jpg'))
06 {
07 $arFiles[] = array('VALUE' => CFile::MakeFileArray($_SERVER["DOCUMENT_ROOT"].'/images/image_'.$i.'.jpg'), 'DESCRIPTION' => '');
08 }
09 }
10 ?>
After that, the array $arFiles is transferred as a property value when an element is added.
Task 5:
Complete a multiple property of the List type with a display as checkboxes. In this case, each element of the list has its own ID. It can be looked up by going to detailed editing of the property. The property shall be completed as follows:
1 <?
2 if($first_condition == true) $values[] = array('VALUE' => 1);
3 if($second_condition == true) $values[] = array('VALUE' => 2);
4 CIBlockElement::SetPropertyValuesEx($ELEMENT_ID, $IBLOCK_ID, array('property_code' => $values));
5 ?>
In this case, when performing the first and second condition, we should check the list elements from ID =1 and ID=2 accordingly. $ELEMENT_ID, $IBLOCK_ID and property_code shall be replaced with necessary values.
Task 6:
Obtain a user property of a section
1 <? $section_props = CIBlockSection::GetList(array(), array('IBLOCK_ID' => IBLOCK_ID, 'ID' => SECTION_ID), true, array("UF_ADDITIONAL_PRICE"));
2 $props_array = $section_props->GetNext(); ?>
Now, the value of the property $props_array['UF_ADDITIONAL_PRICE'] of an infoblock section is located in UF_ADDITIONAL_PRICE.