📜 ⬆️ ⬇️

CMS NetCat - Sorting objects in the table

I propose my solution for sorting the objects of the components presented in the form of a table. I use it often enough, maybe it will be useful to someone.

1. Change the column header to the function that will substitute the desired link

".sort_header('Message_ID','ID')."


here Message_ID is the field by which we will sort, ID is the name of the column
The function sort_header () is described in the default module.
')
function sort_header($fild,$header, $wrap=0){
global $sub,$cc,$sort,$order;
/*
* URI sort, order, sid.
* , .. ,
*
*/
$param="";
$separator='?';
foreach ($_GET as $key => $value) {
if($key!='sort' && $key!='order' && $key!='sid')
{
$param .=$separator.$key."=".$value;
$separator='&';
}
}

// .
if($wrap==0)
{
$out .="";
}

$out .="<a href='$param&sort=$fild";
if($sort==$fild && $order !='ASC')
{
$out .="&order=ASC";
}
$out .="'>$header";
$out .="";
if($sort==$fild && $order =='ASC')
{
$out .=" ";
}
elseif($sort==$fild)
{
$out .=" ";
}
if($wrap==0)
{
$out .="
function sort_header($fild,$header, $wrap=0){
global $sub,$cc,$sort,$order;
/*
* URI sort, order, sid.
* , .. ,
*
*/
$param="";
$separator='?';
foreach ($_GET as $key => $value) {
if($key!='sort' && $key!='order' && $key!='sid')
{
$param .=$separator.$key."=".$value;
$separator='&';
}
}

// .
if($wrap==0)
{
$out .="";
}

$out .="<a href='$param&sort=$fild";
if($sort==$fild && $order !='ASC')
{
$out .="&order=ASC";
}
$out .="'>$header";
$out .="";
if($sort==$fild && $order =='ASC')
{
$out .=" ";
}
elseif($sort==$fild)
{
$out .=" ";
}
if($wrap==0)
{
$out .="
";
}
return $ out;
}


It's simple.
We pass three parameters to the function: the field to be sorted, the name of the column, and the flag, whether or not hyphenation is allowed in the title. By default, they are prohibited.

2. In order for the sorting to work, you need to change the query. To do this, in the settings of the component we write:
$query_order = sort_set();


sort_set () is described in the default module

function sort_set(){
global $inside_admin,$sort,$order,$classID;
// . ,
$sql = "SHOW COLUMNS FROM Message".$classID." WHERE Field = '".$sort."'";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if($num_rows == 1)
{
if($order=='ASC')
{
$order = 'ASC';
}
else
{
$order = 'DESC';
}
// , $query_order
$out="a.".$sort." ".$order;
return $out;
}
else
{
return;
}
}


Everything.
In total, to sort, you need to add two functions to the system, after which the column headers change to call one of them and specify the other in the settings of the component. It seems not difficult. I would be glad if anyone come in handy.

Source: https://habr.com/ru/post/105184/


All Articles