Generate sql from a return of a js

0

Good morning guys over stack. For some time now I've been hammering to find a solution to a problem that has appeared, that nothing comes up in my head.

I have a form, where this form will make a filter in the js datatable. This datatable I use it serverside, so I need sql querys. So far so good, the problem starts with the feedback I receive in php.

It is a return that can vary according to what I have in the form. So that starts my problem, this return varying.

You can get this in php: Desistência de Cadastro,Carta Negada,Carta Cancelada,,,,

How can you get that way too.

Cadastro Iniciado,Aguardando aprovação de cadastro,Cadastro Aprovado,Carta Solicitada,Desistência de Cadastro,Carta Negada,Carta Cancelada,,,MS,MG,PA,PB,PR,PE,PI,RJ,RN,RS,RO,RR,SC,SP, The question is: how to generate an sql from these values.

The query will look something like this:

   SELECT id_parceiro, nome_empresa, cidade, estado, status_atual, ultima_data, data_anterior, status_anterior, dias_entre_status
        FROM  report_partner_view WHERE ativo = 1
        AND (status_atual = 'Cadastro Iniciado' OR status_atual = 'Carta Negada')
        ORDER BY  id_parceiro
                    asc
        LIMIT 0, 10

Being this AND, OR, by state, status, being formed by the return shown above.

Thank you in advance.

    
asked by anonymous 18.04.2017 / 15:22

1 answer

0

PHP can look like this:

$filtro = "Cadastro Iniciado,Aguardando aprovação de cadastro,Cadastro Aprovado,Carta Solicitada,Desistência de Cadastro,Carta Negada,Carta Cancelada,,,MS,MG,PA,PB,PR,PE,PI,RJ,RN,RS,RO,RR,SC,SP,"

$status = explode(',', $filtro);
$status_where = [];
foreach($status as $s) {
   $status_where[] = "'{$s}'";
}
$status_where = implode(',', $status_where);

This will generate an array with items ['Cadastro Iniciado','Aguardando aprovação de cadastro','MS']

And with this array mount your query using a IN

SELECT .. FROM report_partner_view WHERE ativo = 1 AND status_atual IN ($status)
    
18.04.2017 / 20:52