T
T
Tupic1232021-02-27 19:20:52
1C-Bitrix
Tupic123, 2021-02-27 19:20:52

Extra items in search results?

There is a loop that displays the elements of the search results

<?foreach($arResult['SEARCH'] as $arItem):?>
    <div class="news-item">
    <?
$arFilter = Array("IBLOCK_ID"=>$arItem[15], "ID"=>$arItem["ITEM_ID"], "PROPERTY_FILIAL" => $filial,);
$res = CIBlockElement::GetList(Array(), $arFilter);
if ($ob = $res->GetNextElement()){;
    $arFields = $ob->GetFields(); // поля элемента
    $arProps = $ob->GetProperties(); // свойства элемента

   }

?>
<?php  if ($filial == $arProps['FILIAL']['VALUE']) {

  echo '	<a class="service__link" href="">'.$arFields2['NAME'].'</a>
   		
  <p class="service__field">'.$arProps['EMPTY']['VALUE'].'</p>
  <p class="service__field">'.$arProps['NDS']['VALUE'].'</p>
  <p class="service__field">'.$arProps['FILIAL']['VALUE'].'</p>';
} else {
  echo '';
}   ?>
    </div>
  <?endforeach;?>

If a property starts to be used in filtering, then the following happens. The
603a708a08bc5887420691.jpeg
required element is displayed first, then all the rest (apparently equal to the number of search results) begin to be filled in similarly to the last
one . How to make it so that duplicate elements and empty lines are not displayed?
603a70f66859a231766134.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PetrPo, 2021-02-28
@Tupic123

On index.php with a call to the search.page component
1. In the parameter of the FILTER_NAME component, for example, write 2. Before calling the component, add the code
"FILTER_NAME" => "searchFilter"

$itemIds = [];

// здесь укажи ИД своего инфоблока (или массив инфоблоков)
$iblockId = 1;

// здесь значение из get (я наугад написал filial)
$filial = $_GET['filial'];

$iterator = \CIBlockElement::GetList([], ['IBLOCK_ID' => $iblockId, 'PROPERTY_FILIAL' => $filial], false, false, ['ID']);
while($row = $iterator->Fetch()) {
  $itemIds[] = $row['ID'];
}

$GLOBALS['searchFilter'] = ['ITEM_ID' => $itemIds];

That's it, now $arResult['SEARCH'] will already have filtered elements, then you can already get additional fields in result_modifier, it is advisable not to make requests in a cycle.
It is also desirable to cache the code above, an example of how to do this can be found in the file /bitrix/components/bitrix/catalog/templates/bootstrap_v4/section.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question