Views: 9663
Last Modified: 22.09.2014
An example of selection from several infoblocks using page navigation and sorting.
Task
- Select a list of elements fr om several infoblocks and display it as a table;
- Have the possibility to sort the elements;
- Page navigation must work.
Solution
This example can be used if the number of selected elements is not so big (up to 100). Naturally, cache shall be used.
Let us collect all the data into an associative array, such as this one:
[ITEAM] => Array
(
[0] => Array
(
[CITY_NAME] => value
[CITY_DETAIL_URL] => value
[OBJECT_NAME] => value
[OBJECT_ID] => 2487
[DATE_CREATE] => 02.07.2006
[STATUS] => Y
[PAID_STATUS] => Y
[DATEIL_OBJECT_URL] => value
)
[1] => Array
(
[CITY_NAME] => value
[CITY_DETAIL_URL] => value
[OBJECT_NAME] => value
[OBJECT_ID] => 2489
[DATE_CREATE] => 02.07.2006
[STATUS] => Y
[PAID_STATUS] => N
[DATEIL_OBJECT_URL] => value
)
Now, the array $arResult['ITEAM'] must be sorted; for this, we describe the following class:
class CCabinet_SortObject {
function __cmp_ValueOf($a, $b, $name, $order) {
if(is_set($a[$name]) && is_set($b[$name])) {
if($order == 'ASC')
return ($a[$name]<$b[$name])?true:false;
elseif($order == 'DESC')
return ($b[$name]>$a[$name])?false:true;
}
}
function cmp_STATUS_ASC($a, $b) {
return CCabinet_SortObject::__cmp_ValueOf($a, $b, "STATUS", "ASC");
}
function cmp_STATUS_DESC($a, $b) {
return CCabinet_SortObject::__cmp_ValueOf($a, $b, "STATUS", "DESC");
}
function cmp_NAME_ASC($a, $b) {
return CCabinet_SortObject::__cmp_ValueOf($a, $b, "OBJECT_NAME", "ASC");
}
function cmp_NAME_DESC($a, $b) {
return CCabinet_SortObject::__cmp_ValueOf($a, $b, "OBJECT_NAME", "DESC");
}
function cmp_CITY_ASC($a, $b) {
return CCabinet_SortObject::__cmp_ValueOf($a, $b, "CITY_NAME", "ASC");
}
function cmp_CITY_DESC($a, $b) {
return CCabinet_SortObject::__cmp_ValueOf($a, $b, "CITY_NAME", "DESC");
}
function cmp_DATE_DESC($a, $b) {
if ($a["DATE_CREATE"] == $b["DATE_CREATE"]) {
return 0;
}
return ($a["DATE_CREATE"] > $b["DATE_CREATE"]) ? -1 : 1;
}
function cmp_DATE_ASC($a, $b) {
if ($a["DATE_CREATE"] == $b["DATE_CREATE"]) {
return 0;
}
return ($a["DATE_CREATE"] < $b["DATE_CREATE"]) ? -1 : 1;
}
}
Example of class application:
usort($arResult['ITEAM'], array("CCabinet_SortObject", "cmp_".$arParams['SORT_BY']."_".$arParams['SORT_ORDER']));
After that, we have to break down the array by pages using API:
$rs_ObjectList = new CDBResult;
$rs_ObjectList->InitFromArray($arResult['ITEAM']);
$rs_ObjectList->NavStart(10, false);
$arResult["NAV_STRING"] = $rs_ObjectList->GetPageNavString("", 'komka.cabinet');
$arResult["PAGE_START"] = $rs_ObjectList->SelectedRowsCount() - ($rs_ObjectList->NavPageNomer - 1) * $rs_ObjectList->NavPageSize;
while($ar_Field = $rs_ObjectList->Fetch())
{
$arResult['_ITEAM'][] = $ar_Field;
}