public static function GetCount($arFilter = array()) { $DB = App::DB(); $sql = 'SELECT count(*) FROM ' . self::$table; // Фильтр if (empty($arFilter)) { $where = ""; } else { $where = " WHERE "; $where .= filterGlue(filterClear($arFilter)); } $where = str_replace(' GROUP BY ID', '', $where); $res = $DB->Query($sql . $where); if ($result = $DB->Fetch()) { return array_shift($result); } if ($DB->Error()) { dbError($DB->Error()); return false; } }
/** * Генерация SQL-выражения из массивов * @param array $arTables * @param string $arOrder * @param array $arFilter * @param string $arSelect * @param array $arLimit * @return string */ function getListSql($arTables, $arOrder = array(), $arFilter = array(), $arSelect = array(), $arLimit = array()) { // Таблицы $table = ""; if (is_array($arTables)) { if (!empty($arTables)) { $cntTbl = 0; foreach ($arTables as $tableAlias => $tableName) { $cntTbl++; $table .= " `" . $tableName . "` as " . $tableAlias; if ($cntTbl < count($arTables)) { $table .= ", "; } } } else { return false; } } else { if (!empty($arTables)) { $table = $arTables; } else { return false; } } // Порядок if (!$arOrder or empty($arOrder)) { $arOrder = array("SORT" => "ASC"); } $order = getOrderSql($arOrder); // Выборка if (!$arSelect or empty($arSelect)) { if (count($arTables) > 1) { $select = "DISTINCT *"; } else { $select = "*"; } } elseif ($arSelect = "**") { $select = "DISTINCT *"; } else { $select = glueArraySql($arSelect, "S"); } // Фильтр if (!$arFilter or empty($arFilter)) { $where = ""; } else { $where = "WHERE "; $where .= filterGlue($arFilter, $arTables); } // Лимит if (!$arLimit) { $limit = ""; } else { $limit = getLimitSql($arLimit); } $sql = "SELECT " . $select . " FROM " . $table . " " . $where . $order . $limit . ";"; return $sql; }