A
A
Anton2017-09-19 18:57:14
PHP
Anton, 2017-09-19 18:57:14

How to group elements by sections in Bitrix?

<?
$arSelect = Array("ID", "IBLOCK_ID", "IBLOCK_SECTION_ID", "PREVIEW_TEXT", "NAME", "DATE_ACTIVE_FROM","PROPERTY_*");//IBLOCK_ID и ID обязательно должны быть указаны, см. описание arSelectFields выше
$arFilter = Array("IBLOCK_ID"=>38, "ACTIVE_DATE"=>"Y", "ACTIVE"=>"Y");
$res = CIBlockElement::GetList(Array(), $arFilter, false, Array("nPageSize"=>50), $arSelect);
while($ob = $res->GetNextElement()){ 
 $arFields = $ob->GetFields();  
echo "Номер раздела ".$arFields[IBLOCK_SECTION_ID]."</br>Вопрос: ".$arFields[NAME]."<br>Ответ: ".$arFields[PREVIEW_TEXT]."<br><br>";

 $arProps = $ob->GetProperties();
//print_r($arProps);
}
?>

I get all the elements of all sections of the infoblock.
print
Номер раздела 2310
Вопрос: Можно ли вернуть заказ?
Ответ: Да, в течение 14 дней.

Номер раздела 2312
Вопрос: Как часто?
Ответ: Очень часто

Номер раздела 2312

Вопрос: Какие гарантии?
Ответ: 5 лет

How to display the "Section number" (ideally the name) only once, so that it is like this (the section number in the group is displayed once):
Номер раздела 2310
Вопрос: Можно ли вернуть заказ?
Ответ: Да, в течение 14 дней.

Номер раздела 2312
Вопрос: Как часто?
Ответ: Очень часто
                                         <!-- вырезали дубль 2312 -->
Вопрос: Какие гарантии? 
Ответ: 5 лет

and why does IBLOCK_SECTION_ID work, but IBLOCK_SECTION_NAME doesn't?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Tupikov, 2017-09-20
@2amedia

you won’t get by with one request, you will have to do one and one in a loop, which is ugly but not critical
, something like this

$arFilter = Array(
    'IBLOCK_ID' => $IBLOCK_ID,
    'GLOBAL_ACTIVE' => 'Y',
  );
  $arSelect = Array(
    'ID',
    'NAME'
  );
  
  $db_list = CIBlockSection::GetList (Array(), $arFilter, false, $arSelect, false);
  while ($ar_result = $db_list->GetNext ())
  {
    $arSections[]= array('ID'=> $ar_result['ID'], 'NAME'=> $ar_result['NAME']);
  }

foreach ($arSections as $index => $arSection)
{
  $arSelect = Array(
    "ID",
    "IBLOCK_ID",
    "IBLOCK_SECTION_ID",
    "PREVIEW_TEXT",
    "NAME",
    "DATE_ACTIVE_FROM",
    "PROPERTY_*"
  );
  $arFilter = Array(
    "IBLOCK_ID" => 38,
    "ACTIVE_DATE" => "Y",
    "ACTIVE" => "Y",
    'IBLOCK_SECTION_ID' => $arSection['ID']
  );
  $res = CIBlockElement::GetList (Array(), $arFilter, false, Array(), $arSelect);
  while ($ob = $res->GetNextElement ())
  {
    $arItem = $ob->GetFields ();
    $arItem['PROPS'] = $ob->GetProperties ();
    $arItems[] = $arItem;
  }
  $arFullSection = $arSection;
  $arFullSection['ITEMS'] = $arItems;
  $arFullSection[] = $arFullSection;
unset($arItem,$arItems,$arFullSection);
}

arFullSection - will contain all the necessary data

A
Andrey Nikolaev, 2017-09-21
@gromdron

But did not try to use the standard grouping feature: arGroupBy (3rd parameter in getList, which you have set to false)

An array of fields to group the element. If the fields are specified, then the selection is grouped by them (in this case, the arSelectFields parameter will be ignored), and the CNT field is added to the result - the number of grouped elements. If you specify an empty array as arGroupBy, the method will return the number of CNT elements by filter. You can group by the fields of the element, as well as by the values ​​of its properties. To do this, you must specify PROPERTY_ as one of the grouping fields, where PROPERTY_CODE is the ID or symbolic code of the property.
Optional. Default is false - records are not grouped.

Group by section ID.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question