Browse Source

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

fusionate 1 year ago
parent
commit
6f1e853178
1 changed files with 14 additions and 5 deletions
  1. 14 5
      resources/functions.php

+ 14 - 5
resources/functions.php

@@ -2003,13 +2003,17 @@ 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 = strtolower($dir) == 'desc' ? 'desc' : 'asc';
+			$dir = !empty($dir) && strtolower($dir) == 'desc' ? 'desc' : 'asc';
 			if (!empty($col)) {
 			if (!empty($col)) {
-				return $order_by.$col.' '.$dir.' ';
+				if ($sort == 'natural') {
+					return $order_by.'natural_sort('.$col.') '.$dir.' ';
+				}
+				else {
+					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) {
@@ -2022,7 +2026,12 @@ function number_pad($number,$n) {
 					}
 					}
 				}
 				}
 				else {
 				else {
-					return $order_by.$col_default.' '.$dir_default.' ';
+					if ($sort == 'natural') {
+						return $order_by.'natural_sort('.$col_default.') '.$dir_default.' ';
+					}
+					else {
+						return $order_by.$col_default.' '.$dir_default.' ';
+					}
 				}
 				}
 			}
 			}
 		}
 		}