V
V
Vladimir2021-07-23 14:52:50
1C-Bitrix
Vladimir, 2021-07-23 14:52:50

Bitrix API. How to display one element from different sections of the infoblock?

Hello gentlemen.
There is a task - to display one last added element (in my case, the name of the element and a link to the file) from the specified sections of the infoblock.

I use CIBlockElement::GetList, but the documentation says that the number of elements can be specified by the CNT key in ArOrder BUT only with a given grouping. Tried to enable grouping by SECTION_ID - gives MySQL error.

Maybe there is a more elegant solution?

<?
$arSelect = Array("ID", "IBLOCK_ID", "NAME", "PROPERTY_FILE");//IBLOCK_ID и ID обязательно должны быть указаны, 
$arFilter_old = Array(
  "IBLOCK_ID"=>390,   // id инфоблока
  "SECTION_ID" => array(3768, 3769, 3770, 3771, 3772, 3773, 3774, 3775, 3776), //разделы
  'ACTIVE' => 'Y',  // фильтруем по активности
);

$arOrder = Array('ID' => 'ASC');

$res = CIBlockElement::GetList($arOrder, $arFilter_old, false, Array("nPageSize"=>10), $arSelect);
while($ob = $res->GetNextElement()){ 
  //получаем поля (которые указали в $arSelect)
  $arFields = $ob->GetFields();  
  echo '<a href="'.CFile::GetPath($arFields["PROPERTY_FILE_VALUE"]).'">'.$arFields["NAME"].'</a><br>';
}
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-07-24
@Dr_Boo

Forget groupings.
Most likely, simply iterating over the sections and GetList for each will be much faster than any attempt to construct a general query.
The solution is not elegant, not beautiful, but fast, clear and working - just iterate over the sections and do a GetList for each with a limit of 1 and sort by ID - these will be very simple and fast queries.
I think it's always worth remembering that your code should work, not hang in a frame on the wall of an art gallery.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question