V
V
Vladimir2016-12-22 14:22:35
PHP
Vladimir, 2016-12-22 14:22:35

How to set up a news filter in Bitrix?

Good afternoon!
Bitrix site. There is a news page. You need a filter by years with select and option.
Here is my filter code.
<?
$arrNews = CIBlockElement::GetList(array('ACTIVE_FROM'=>'DESC'),array('IBLOCK_ID'=>14, 'ACTIVE'=>'Y', 'ACTIVE_DATE'=>'Y'));
while ($prodtype = $arrNews->GetNext()):
// write to the array of years and months in year-month format
$arrYear[] = substr($prodtype['ACTIVE_FROM'],6,4).' -'.substr($prodtype['ACTIVE_FROM'],3,2);
endwhile;
//Remove duplicate elements from the array
$resultYear = array_keys(array_count_values($arrYear));
$resultYear = array_reverse($resultYear);
?>
select onchange="window.location=this.value"
<?
// Display the years for which there is news
for($i = 0; $i < sizeof($resultYear); $i++):
$cYear = substr($resultYear[$i],0,4);// Select the year
if ($cc == $cYear): // If the year has not yet repeated, print it out !=
//$linkYear = $cYear; // Rewrite to form links
continue;
endif;
$linkYear = $cYear;
//Output year with links
echo 'option value="'.SITE_DIR.'/news/?year='.$linkYear.'" '.$cYear.'/option';
$cc = $cYear; // Rewrite to control the repetition of the year
endfor;
?>
/select
The problem is with select and option.
The site contains news for 2015 and 2016. On the start page, this filter is for 2016. I click on my dropdown list, click 2015 and it shows the news for 2015. But the value of the select box itself does not change (should show: 2015), it also remains for 2016. And I can’t go to the news of 2016 anymore.
I do not understand what to do to make the filter work correctly. Help me please.
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita, 2016-12-22
@Rema1ns

Of course, a strange decision, and it will not be friends with the cache either. Why not take a complex news component?
In order to help you, you need to know in which components which code is executed. And how do you generate select over the years, most likely you do not have a condition for the current year that will make the desired option active.

V
VladimirBolotov, 2016-12-22
@VladimirBolotov

This is a crutch for bitrix.news.list. I just put this code in result modifire and that's it.
I have another option.
<?
$arSelect = Array("DATE_ACTIVE_FROM");
$arFilter = Array("IBLOCK_ID"=>$arParams["IBLOCK_ID"], "ACTIVE_DATE"=>"Y", "ACTIVE"=>"Y");
$res = CIBlockElement::GetList(Array(), $arFilter, false, Array(), $arSelect);
while($ob = $res->GetNextElement()) {
$arFields = $ob->GetFields();
$years[] = date("Y", strtotime($arFields['DATE_ACTIVE_FROM']));
}
$years = array_unique($years);
array($years);
?>
select onchange="window.location=this.value"
< ?
foreach($years as $year) {
echo 'option value="'.$APPLICATION->GetCurPage().'?year='.$year.'">'.$year.'/option';
}
?>
/select
I write this code in the bitrix news list template

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question