S
S
SteepNET2019-12-26 01:34:18
1C-Bitrix
SteepNET, 2019-12-26 01:34:18

Displaying the required number of elements on the Bitrix page?

Good afternoon! There is a method that displays a given number of elements.
Before calling bitrix:catalog.section

<?
$pageElementCount = "10";
if (array_key_exists("showBy", $_REQUEST)) {
  if ( intVal($_REQUEST["showBy"]) && in_array(intVal($_REQUEST["showBy"]), array(18, 36, 54, 72)) ) {
    $pageElementCount = intVal($_REQUEST["showBy"]);
    $_SESSION["showBy"] = $pageElementCount;
  } elseif ($_SESSION["showBy"]) {
    $pageElementCount = intVal($_SESSION["showBy"]);
  }
}
?>

In component
"PAGE_ELEMENT_COUNT" => "$pageElementCount",
In template
<div class="text-right">
                    <span class="show_title">Показать по </span>
                    <span class="number_list">
                <? for ($i = 18; $i <= 72; $i+=18) : ?>
                    <a rel="nofollow" <? if ($i == $pageElementCount): ?>class="current"<? endif; ?>
                         href="<?= $APPLICATION->GetCurPageParam('showBy=' . $i, array('showBy', 'mode')) ?>"
                    >
                        <span><?= $i ?></span>
                    </a>
                <? endfor; ?>
                </span>
                </div>

How to get rid of the for() loop, just set fixed values?
And it seems that values ​​are not driven into the session, am I doing it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PetrPo, 2019-12-26
@SteepNET

It is written to the session, checked, but it is not clear why exactly to write to the session and the logic as a whole ...

По умолчанию $pageElementCount = "10", далее
Если в $_REQUEST есть showBy, то 
        Если showBy соответствует значению 18, 36, 54, 72,
                pageElementCount = $_REQUEST['showBy']
                пишем в $_SESSION
        Если не соответствует
                 pageElementCount = $_SESSION['showBy']

Well, we wrote showBy in $_SESSION, then we go to another page of the catalog or we remove the showBy parameter from the address bar, then $pageElementCount becomes 10 again.
Question. What then sense to write down in session?
But in general, although the code is not beautiful and with incomprehensible logic, it works)))
My version:
$arAvailableCounts = array(18, 36, 54, 72);

$pageElementCount = isset($_COOKIE['showBy']) && intval($_COOKIE['showBy']) ? $_COOKIE['showBy'] : $arParams['PAGE_ELEMENT_COUNT'];

if(isset($_GET['showBy']) && $_GET['showBy'] && in_array($_GET['showBy'], $arAvailableCounts)) {
  $pageElementCount = $_GET['showBy'];
  setcookie('showBy', $_GET['showBy'], 0, SITE_DIR);
}

<div class="text-right">
  <span class="show_title">Показать по </span>
  <span class="number_list">
    <?foreach($arAvailableCounts as $value):?>
      <a class="<?=($value == $pageElementCount ? 'current' : '');?>" href="<?=$APPLICATION->GetCurPageParam('showBy='.$value, array('showBy'));?>" rel="nofollow"><span><?=$value;?></span></a>
    <?endforeach;?>
  </span>
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question