• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/general/dbresult.php
  • Класс: CAllDBResult
  • Вызов: CAllDBResult::NavQuery
function NavQuery($strSql, $cnt, $arNavStartParams, $bIgnoreErrors = false)
{
	global $DB;

	if (isset($arNavStartParams["SubstitutionFunction"]))
	{
		$arNavStartParams["SubstitutionFunction"]($this, $strSql, $cnt, $arNavStartParams);
		return null;
	}

	$bDescPageNumbering = $arNavStartParams["bDescPageNumbering"] ?? false;

	$this->InitNavStartVars($arNavStartParams);
	$this->NavRecordCount = $cnt;

	if ($this->NavShowAll)
	{
		$this->NavPageSize = $this->NavRecordCount;
	}

	//calculate total pages depend on rows count. start with 1
	$this->NavPageCount = ($this->NavPageSize > 0 ? floor($this->NavRecordCount / $this->NavPageSize) : 0);
	if ($bDescPageNumbering)
	{
		$makeweight = 0;
		if ($this->NavPageSize > 0)
		{
			$makeweight = ($this->NavRecordCount % $this->NavPageSize);
		}
		if ($this->NavPageCount == 0 && $makeweight > 0)
		{
			$this->NavPageCount = 1;
		}

		//page number to display
		$this->calculatePageNumber($this->NavPageCount);

		//rows to skip
		$NavFirstRecordShow = 0;
		if ($this->NavPageNomer != $this->NavPageCount)
		{
			$NavFirstRecordShow += $makeweight;
		}

		$NavFirstRecordShow += ($this->NavPageCount - $this->NavPageNomer) * $this->NavPageSize;
		$NavLastRecordShow = $makeweight + ($this->NavPageCount - $this->NavPageNomer + 1) * $this->NavPageSize;
	}
	else
	{
		if ($this->NavPageSize > 0 && ($this->NavRecordCount % $this->NavPageSize > 0))
		{
			$this->NavPageCount++;
		}

		//calculate total pages depend on rows count. start with 1
		$this->calculatePageNumber(1, true, (bool)($arNavStartParams["checkOutOfRange"] ?? false));
		if ($this->NavPageNomer === null)
		{
			return null;
		}

		//rows to skip
		$NavFirstRecordShow = $this->NavPageSize * ($this->NavPageNomer - 1);
		$NavLastRecordShow = $this->NavPageSize * $this->NavPageNomer;
	}

	$NavAdditionalRecords = 0;
	if (is_set($arNavStartParams, "iNavAddRecords"))
	{
		$NavAdditionalRecords = $arNavStartParams["iNavAddRecords"];
	}

	if (!$this->NavShowAll)
	{
		$strSql .= " LIMIT " . ($NavLastRecordShow - $NavFirstRecordShow + $NavAdditionalRecords) . " OFFSET " . $NavFirstRecordShow;
	}

	if (is_object($this->DB))
	{
		$res_tmp = $this->DB->Query($strSql, $bIgnoreErrors);
	}
	else
	{
		$res_tmp = $DB->Query($strSql, $bIgnoreErrors);
	}

	// Return false on sql errors (if $bIgnoreErrors == true)
	if ($bIgnoreErrors && ($res_tmp === false))
	{
		return false;
	}

	$this->result = $res_tmp->result;
	$this->DB = $res_tmp->DB;

	if ($this->SqlTraceIndex)
	{
		$start_time = microtime(true);
	}

	$temp_arrray = [];
	$temp_arrray_add = [];
	$tmp_cnt = 0;

	while ($ar = $this->FetchInternal())
	{
		$tmp_cnt++;
		if (intval($NavLastRecordShow - $NavFirstRecordShow) > 0 && $tmp_cnt > ($NavLastRecordShow - $NavFirstRecordShow))
		{
			$temp_arrray_add[] = $ar;
		}
		else
		{
			$temp_arrray[] = $ar;
		}
	}

	if ($this->SqlTraceIndex)
	{
		/** @noinspection PhpUndefinedVariableInspection */
		$exec_time = round(microtime(true) - $start_time, 10);
		$DB->addDebugTime($this->SqlTraceIndex, $exec_time);
		$DB->timeQuery += $exec_time;
	}

	$this->arResult = (!empty($temp_arrray) ? $temp_arrray : false);
	$this->arResultAdd = (!empty($temp_arrray_add) ? $temp_arrray_add : false);
	$this->nSelectedCount = $cnt;
	$this->bDescPageNumbering = $bDescPageNumbering;
	$this->bFromLimited = true;

	return null;
}