N
N
Night3862021-10-23 13:03:51
PHP
Night386, 2021-10-23 13:03:51

How to sort?

Good afternoon! there is this code

$limit = 'limit 0, 500';

        if (isset($_GET['ajax2'])) $limit = 'limit '.(int)$_GET['offset'].', 30';

        $rows = fs::getObjects($sql, "order by c_object.added desc ".$limit);
        $rowsCount = count(fs::getObjects($sql));

        if (isset($_GET['ajax2']) && !count($rows))
        {
          header("HTTP/1.0 404 Not Found");
          die;
        }


Tell me how you can make it so that there are two selecta (or maybe some kind of toggle switch) - one that is available in the code ("order by c_object.added desc) - this is sorting by added, and the second changes to ("order by c_object.updated desc ) - sort by updated . there is a php file and a tpl file

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alexalexes, 2021-10-23
@alexalexes

Add two more parameters to the sort part of the query.
order_column - which column to sort by, order_direct - in which direction.
The limit example hints at how to do this.

$limit = 'limit 0, 500';
$order_column = 'added';
$order_direct = 'desc';
if(isset($_GET['order_col']))
{
  if($_GET['order_col'] == 'updated')
    $order_column = 'updated'; // не вздумайте подставлять из GET название колонки, будет sql-инъекция!
}
if(isset($_GET['order_dir']))
{
  if($_GET['order_dir'] == 'asc')
    $order_direct = 'asc'; // не вздумайте подставлять из GET название клаузы, будет sql-инъекция!  
}
        if (isset($_GET['ajax2'])) $limit = 'limit '.(int)$_GET['offset'].', 30'; // тут застраховано от инъекции при помощи преобразования в int!

        $rows = fs::getObjects($sql, "order by c_object.".$order_column." ".$order_direct." ".$limit);
        $rowsCount = count(fs::getObjects($sql));

        if (isset($_GET['ajax2']) && !count($rows))
        {
          header("HTTP/1.0 404 Not Found");
          die;
        }

PS: It remains for you to add to the question, "where can I find a form template with a list filter to set order_col and order_dir get-parameters".

R
runapa, 2021-10-24
@runapa

function orderBy(array &$array, $sortOrder)
{
    usort($array, function ($a, $b) use ($sortOrder) {
        $result = '';

        $sortOrderArray = explode(',', $sortOrder);
        foreach ($sortOrderArray AS $item) {
            $itemArray = explode(' ', trim($item));
            $field = $itemArray[0];
            $sort = !empty($itemArray[1]) ? $itemArray[1] : '';

            $mix = [$a, $b];
            if (!isset($mix[0][$field]) || !isset($mix[1][$field])) {
                continue;
            }

            if (strtolower($sort) === 'desc') {
                $mix = array_reverse($mix);
            }

            if (is_numeric($mix[0][$field]) && is_numeric($mix[1][$field])) {
                $result .= ceil($mix[0][$field] - $mix[1][$field]);
            } else {
                $result .= strcasecmp($mix[0][$field], $mix[1][$field]);
            }
        }

        return $result;
    });

    return $array;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question