L
L
Leopandro2016-02-09 18:29:57
Yii
Leopandro, 2016-02-09 18:29:57

How to edit such code? (or how to write the most idiotic code)?

foreach ($params as $param)
        {
            if ($param['label'] != 'name'){
                if ($param['type_value'] == '1') {
                    if ($param['value'] != '')
                        $query->where(['like', $param['label'], $param['value']]);
                }
                else {
                    if(($param['firstcompare'] == '<' || $param['firstcompare'] == '<=' || $param['firstcompare'] == '=') && $param['firstvalue'] != '') {
                        if($param['secondcompare'] != '' && $param['secondvalue'] != ''){
                            $query->where([
                                'or',
                                $param['label'].$param['firstcompare'].$param['firstvalue'],
                                $param['label'].$param['secondcompare'].$param['secondvalue']
                            ]);
                               // ->andWhere([$param['secondcompare'], $param['label'], $param['secondvalue']]);
                        }
                        else{
                            $query->where([$param['firstcompare'], $param['label'], $param['firstvalue']]);
                        }
                    }
                    elseif($param['firstcompare'] == '>' || $param['firstcompare'] == '>=' && $param['firstvalue'] != '')
                    {
                        if($param['secondcompare'] != '' && $param['secondvalue'] != ''){
                            $query->where([
                                'and',
                                $param['label'].$param['firstcompare'].$param['firstvalue'],
                                $param['label'].$param['secondcompare'].$param['secondvalue']
                            ]);
                        }
                        else{
                            $query->where([$param['firstcompare'], $param['label'], $param['firstvalue']]);
                        }
                    }
                    elseif($param['firstcompare'] == '' || $param['firstvalue'] == '') {
                        if ($param['secondcompare'] != '' || $param['secondvalue'] != '')
                            $query->where([$param['secondcompare'], $param['label'], $param['secondvalue']]);
                    };
                }
            }
        }

This is the function that parses this case:
02abe6d3dcec4a5d9f21db20648b02a3.jpg
(The client asked that if the first comparison is < and the second > then there should be a search for "and" if the first value is greater than the second and I have if < and > search by "or" is used, because according to my assumption must be < 1 and > 2 to exclude from the result what is between 1 and 2, and he enters the first value 2 and the second 1 ... )

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pibiv, 2016-02-09
@pibiv

Javascript that will unlock the input of the second field only when the first one is filled, does this option not work?

A
Alexander Kubintsev, 2016-02-09
@akubintsev

For such cases, they have long been invented in various normal Forms frameworks

A
Alexander Makarov, 2016-02-09
@SamDark

Move it to a separate class, write a test for it. Then you can comb as much as you like and see if the test has broken.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question