Manipulating a key from an array?

Asked

Viewed 56 times

0

I need to manipulate a key which is passes as parameter in this query:

$Read->FullRead("SELECT DISTINCT ".DB_CONTAS." WHERE 
                 contas_status = 1 `{$FilterAdd} ", "{$FilterValues}");`

The var_dump {$FilterAdd} return that:

array (size=3)
  'contastipodetalhe' => string '1' (length=1)
  'anobase' => string '2016' (length=4)
  'mesbase' => string '12' (length=2)

Well, what’s complicated about it?

I need that if the user selects 1 or more filter fields, that my array only receive the fields that have been selected and if any of these fields is anobase and/or mesbase, I have to insert a year and month in select to extract the year and month so that it can compare to the value selected in the field.

Example:

My query current looks like this:

SELECT DISTINCT contas 
 WHERE contas_status = 1 AND 
       contastipodetalhe = :contastipodetalhe AND 
       anobase = :anobase AND mesbase = :mesbase '

I need her to stay that way:

SELECT DISTINCT contas 
 WHERE contas_status = 1 AND 
       contastipodetalhe = :contastipodetalhe AND 
       year(anobase) = :anobase AND month(mesbase) = :mesbase '

Note that I use year and month, that I am not managing to send in the vector at this time that it mounts the {$FilterAdd}:

if ($TranspFilter):     
    foreach ($TranspFilter as $fKey => $fValue):     
           $FilterAdd .= " AND {$fKey} = :{$fKey}";     
    endforeach;
    $_SESSION['transp_filter'] = null;
endif; 

I’ve thought about making one if to know if the selected value is month or year and then try to change the key of vetor, but it didn’t, if anyone knows where I went wrong or give me another solution I appreciate:

if ($TranspFilter):     
    foreach ($TranspFilter as $fKey => $fValue):
      if($fKey['anobase']):
          $FilterAdd .= "AND year({$fKey}) = :({$fKey})";
      endif;
      if($fKey['mesbase']):
          $FilterAdd .= "AND month({$fKey}) = :({$fKey})";
      endif;     
      $FilterAdd .= " AND {$fKey} = :{$fKey}";     
    endforeach;
    $_SESSION['transp_filter'] = null;
endif;

1 answer

2


Your code is incorrect. You are treating the key(string) as if it were an array.

Correcting:

if ($TranspFilter) {

    foreach ($TranspFilter as $fKey => $fValue) {
        if ($fKey == 'anobase') {
            $FilterAdd .= "AND year({$fKey}) = :({$fKey})";
        } else if ($fKey == 'mesbase') {
            $FilterAdd .= "AND month({$fKey}) = :({$fKey})";
        } else {
            $FilterAdd .= " AND {$fKey} = :{$fKey}";
        }
    }
    $_SESSION['transp_filter'] = null;

}

PS: I rewrote the blocks in the conventional way, since there is no HTML block and I don’t think it’s worth losing the readability of the code written this way for nothing.

  • quase......
após aplicar seu código, duplicou as querys, veja o resultado:

'SELECT DISTINCT contas WHERE contas_status = 1 AND contastipodetalhe = :contastipodetalhe AND year(anobase) = :(anobase) AND anobase = :anobase AND month(mesbase) = :(mesbase) AND mesbase = :mesbase ' Note that appeared 2 times the year(anobase) and Month(mesbase)

  • Corrected. Missing an 'Else'.

  • 1

    worked out. thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.