A
A
alexander_chn2019-11-13 16:20:00
API
alexander_chn, 2019-11-13 16:20:00

How to display catalog items randomly after outputting the first 20 sorted in Bitrix?

Hello! The task is next. In the catalog, the first 20 items are displayed sorted by the SORT field. Starting from 21 elements (there may be about 100 cards) it should be displayed randomly. And after them, briefs are already displayed by the date of creation. How to solve this problem. I don't even have any ideas at the moment. I would appreciate any thoughts, thanks!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alams Stoyne, 2019-11-13
@alams_stoyne

Step 1: Get the first 20 elements,
Step 2: Get 100 random elements by removing 20 from them obtained in Step 1,
Step 3: Get the remaining elements by removing 120 from them obtained as a result of Step 1 and Step 2,

\Bitrix\Main\Loader::includeModule('iblock');

// Шаг 1:  Получаем первые 20 элементов,

    $arSelect = Array("ID","NAME","DETAIL_PAGE_URL");
    $nPageSize = Array("nPageSize" => 20);
    $arSort = Array("SORT" => "ASC");

    $arFilter = Array("IBLOCK_ID"=>IntVal($this->arParams['IBLOCK_ID']), "ACTIVE"=>"Y");
    $res = CIBlockElement::GetList($arSort, $arFilter, false, $nPageSize, $arSelect);   
    $ALL_RESULT = [];
    $IGNORE_IDS = [];
    while($ob = $res->GetNextElement())
    {         
        $GetFields = $ob->GetFields();
        $IGNORE_IDS[] = $GetFields["ID"];
        $ALL_RESULT[] = $GetFields;
    }

// Шаг 2:  Получаем 100 случайных элементов убрав из них 20 полученные в Шаге 1,

    $nPageSize = Array("nPageSize" => 100);
    $arSort = Array("RAND" => "ASC");
    $arFilter = Array("!ID" => $IGNORE_IDS, "IBLOCK_ID"=>IntVal($this->arParams['IBLOCK_ID']), "ACTIVE"=>"Y");
    $res = CIBlockElement::GetList($arSort, $arFilter, false, $nPageSize, $arSelect);   
    while($ob = $res->GetNextElement())
    {         
        $GetFields = $ob->GetFields();
        $IGNORE_IDS[] = $GetFields["ID"];
        $ALL_RESULT[] = $GetFields;
    }

// Шаг 3:  Получаем оставшиеся элементы убрав из них 120 полученные в результате Шаг 1 и Шаг 2,
    $nPageSize = Array();
    $arSort = Array("ACTIVE_FROM" => "ASC");
    $arFilter = Array("!ID" => $IGNORE_IDS, "IBLOCK_ID"=>IntVal($this->arParams['IBLOCK_ID']), "ACTIVE"=>"Y");
    $res = CIBlockElement::GetList($arSort, $arFilter, false, $nPageSize, $arSelect);   
    while($ob = $res->GetNextElement())
    {         
        $GetFields = $ob->GetFields();
        $IGNORE_IDS[] = $GetFields["ID"];
        $ALL_RESULT[] = $GetFields;
    }
    return $ALL_RESULT;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question