Order By [Function]: Updated to support natural sorting.

This commit is contained in:
fusionate 2023-10-19 16:46:48 +00:00
parent 82f82bd87c
commit 0de8570dd3
No known key found for this signature in database
1 changed files with 14 additions and 5 deletions

View File

@ -2003,14 +2003,18 @@ function number_pad($number,$n) {
//validate and format order by clause of select statement //validate and format order by clause of select statement
if (!function_exists('order_by')) { if (!function_exists('order_by')) {
function order_by($col, $dir, $col_default = '', $dir_default = 'asc') { function order_by($col, $dir, $col_default = '', $dir_default = 'asc', $sort = '') {
$order_by = ' order by '; $order_by = ' order by ';
$col = preg_replace('#[^a-zA-Z0-9-_.]#', '', $col ?? ''); $col = preg_replace('#[^a-zA-Z0-9-_.]#', '', $col ?? '');
if(!empty($dir)) $dir = !empty($dir) && strtolower($dir) == 'desc' ? 'desc' : 'asc';
$dir = strtolower($dir) == 'desc' ? 'desc' : 'asc';
if (!empty($col)) { if (!empty($col)) {
if ($sort == 'natural') {
return $order_by.'natural_sort('.$col.') '.$dir.' ';
}
else {
return $order_by.$col.' '.$dir.' '; return $order_by.$col.' '.$dir.' ';
} }
}
else if (!empty($col_default)) { else if (!empty($col_default)) {
if (is_array($col_default) && @sizeof($col_default) != 0) { if (is_array($col_default) && @sizeof($col_default) != 0) {
foreach ($col_default as $k => $column) { foreach ($col_default as $k => $column) {
@ -2021,12 +2025,17 @@ function number_pad($number,$n) {
return $order_by.implode(', ', $order_bys); return $order_by.implode(', ', $order_bys);
} }
} }
else {
if ($sort == 'natural') {
return $order_by.'natural_sort('.$col_default.') '.$dir_default.' ';
}
else { else {
return $order_by.$col_default.' '.$dir_default.' '; return $order_by.$col_default.' '.$dir_default.' ';
} }
} }
} }
} }
}
//validate and format limit and offset clause of select statement //validate and format limit and offset clause of select statement
if (!function_exists('limit_offset')) { if (!function_exists('limit_offset')) {