M
M
Maxim Morozov2020-01-15 12:13:45
1C-Bitrix
Maxim Morozov, 2020-01-15 12:13:45

How to display the number of views of an element in the Bitrix product card with the function of returning the end of the word when declining?

Greetings! I'm trying to display the number of views of an element (product) in the product card.
It should look something like this: Viewed: "value" person(s).
But there is one thing, but, I'm still just starting to learn php and I'm apparently doing something wrong.
When outputting, instead of Man or Man, it displays the number 1.

$resElement = CIBlockElement::GetByID($arResult['ID'])->GetNext();
            $value = $resElement;
            if(!function_exists('BITGetDeclNum'))
              {
                  function BITGetDeclNum($value=1, $status= array('Человек','Человека','Человек'))
              {
                  $array =array(2,0,1,1,1,2);
                  return $status[($value%100>4 && $value%100<20)? 2 : $array[($value%10<5)?$value%10:5]];
              }
             
              }
              
              echo 'Просмотрело: '.$resElement["SHOW_COUNTER"], print_r("&nbsp;" + $status);

Can someone help?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton, 2020-01-15
@morozoffmaks

function RusEnding($n, $n1, $n2, $n5) {
    if($n >= 11 and $n <= 19) return $n5;
    $n = $n % 10;
    if($n == 1) return $n1;
    if($n >= 2 and $n <= 4) return $n2;
    return $n5;
  }
echo RusEnding(40, "Человек", "Человека", "Человек");

V
Viktor Krasnov, 2020-11-10
@katzo

use Bitrix\Main\Grid\Declension;
echo (new Declension('штука', 'штуки', 'штук'))->get(2);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question