Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.

qa_sort_by_fn in sort.php error

0 votes
24 views
asked Dec 4 in Q2A Core by delstone
The  function qa_sort_by_fn($a, $b) seems to generate an error "Notice: Undefined index:created-asc in /var/www/qa/qa-include/util/sort.php line 50" whereby the following fix is proposed.

My query is that should this fix be made to the core file (which can be lost if there is an upgrade) or should a function override or something similar in a plugin which is causing errors to occur be used? Or should the source of the error be identified?
/*
    Function used in uasort to implement qa_sort_by()
*/
//    function qa_sort_by_fn($a, $b)
///*
//    Function used in uasort to implement qa_sort_by()
//*/
//    {
//        global $qa_sort_by_1, $qa_sort_by_2;
//
//        $compare=qa_sort_cmp($a[$qa_sort_by_1], $b[$qa_sort_by_1]);
//
//        if (($compare==0) && $qa_sort_by_2)
//            $compare=qa_sort_cmp($a[$qa_sort_by_2], $b[$qa_sort_by_2]);
//
//        return $compare;
//    }
        
        
  function qa_sort_by_fn($a, $b) {
    global $qa_sort_by_1, $qa_sort_by_2;

    // if the first keys are equal we can sort by the second keys only
    $sortkey = $qa_sort_by_1;

    // MODIFICATION x2 lines
    if (!isset($a[$sortkey]))
        $a[$sortkey] = '';
    if (!isset($b[$sortkey]))
        $b[$sortkey] = '';

    if ($a[$sortkey] == $b[$sortkey]) {
        if (!isset($qa_sort_by_2))
            return 0;

        $sortkey = $qa_sort_by_2;
    }

    $av = $a[$sortkey];
    $bv = $b[$sortkey];

    if (is_numeric($av) && is_numeric($bv)) // straight subtraction won't work for floating bits
        return $av == $bv ? 0 : ($av < $bv ? -1 : 1);
    else
        return strcasecmp($av, $bv); // doesn't do UTF-8 right but it will do for now
}
Q2A version: 1.7.5
commented 4 days ago by pupi1985
In order to prove your point just provide the 4 sample input pieces of data that result in the notice so that other people can replicate the issue. If you can additionally explain the actions that a user needs to take to to get to the error, that will also help a lot.

Please log in or register to answer this question.

...