-1

why the IF(the lastest one with else if and else) is doing all the time only first condition and only the first part ($filtry_1value[$key] = 'min_cena'), even if the condition shouldnt be true. I have another solution (less dynamic), if I will not fix this one, but I would like to know, why it is not working... I think it will be a trivial thing, but I cannot see it.

PS: I am working with laravel.

$filtry_1value = ['stat', 'lokalita', 'patro', 'min_cena', 'max_cena', 'min_uzitna_plocha', 'max_uzitna_plocha'];

  foreach ($filtry_1value as $key => $filtr_1value) {
  $filtr_1value = \Request::has($filtr_1value) ? \Request::get($filtr_1value) : null;
    if(!empty($filtr_1value)){
      if ($filtry_1value[$key] = 'min_cena' OR $filtry_1value[$key] = 'min_uzitna_plocha') {
        $query->where(substr($filtry_1value[$key], 4),'>=',$filtr_1value);
      }
      elseif ($filtry_1value[$key] = 'max_cena' OR $filtry_1value[$key] = 'max_uzitna_plocha') {
        $query->where(substr($filtry_1value[$key], 4),'<=',$filtr_1value);
      }
      else {
        $query->where($filtry_1value[$key],'=', $filtr_1value);
      }
    }
  }
paklic9
  • 7
  • 1

2 Answers2

2

may be-

foreach ($filtry_1value as $key => $filtr_1value) {
  $filtr_1value = \Request::has($filtr_1value) ? \Request::get($filtr_1value) : null;
    if(!empty($filtr_1value)){
      if ($filtry_1value[$key] == 'min_cena' OR $filtry_1value[$key] == 'min_uzitna_plocha') {
        $query->where(substr($filtry_1value[$key], 4),'>=',$filtr_1value);
      }
      elseif ($filtry_1value[$key] == 'max_cena' OR $filtry_1value[$key] == 'max_uzitna_plocha') {
        $query->where(substr($filtry_1value[$key], 4),'<=',$filtr_1value);
      }
      else {
        $query->where($filtry_1value[$key],'=', $filtr_1value);
      }
    }
  }
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Ilya Yaremchuk
  • 2,007
  • 2
  • 19
  • 36
0

You need to use the double equal sign for comparisons. == not a single =

Your if's should look like:-

if ($filtry_1value[$key] == 'min_cena' OR $filtry_1value[$key] == 'min_uzitna_plocha') {
    // ...
} elseif ($filtry_1value[$key] == 'max_cena' OR $filtry_1value[$key] == 'max_uzitna_plocha') {
    // ...
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
DACrosby
  • 11,116
  • 3
  • 39
  • 51