How to do two checks on select

0

I have 3 selects in my code where:

  • If the user enters the start date and end date, they will display the data between them.
  • If the user enters the invoice it will show the data with that note.
  • If the user selects the peripheral it will show all the data with that name registered.

Now my question is:  How to make the select stay:

  • If you select start date / end date and peripheral , peripherals (with the name informed) registered within the dates defined?
  • If you select invoice and peripheral just show the selected peripheral of that note!

Selection code:

if($w_op == "P"){ //P é o periférico
    $w_query="where sai_tb_peri.seq_peri = '$w_cb_peri'";   
}
else
if($w_op == "N"){ //N é a nota fiscal
    $w_query="where sai_cad_nf.num_nf = '$w_tx_nota'";
}
else{ //Caso for nenhum é as datas
$w_query="where sai_cad_nf.dt_nota_fisc between '$w_tx_dt_inic' and '$w_tx_dt_fina'";
}

$w_querybusca="SELECT * FROM sai_cad_cara_peri
                    INNER JOIN sai_tb_peri on sai_cad_cara_peri.fk_seq_peri = sai_tb_peri.seq_peri
                    INNER JOIN sai_cad_nf ON sai_cad_cara_peri.fk_seq_nf = sai_cad_nf.seq_nf $w_query;";
    
asked by anonymous 05.11.2014 / 13:37

2 answers

1

I would not put these else's at first, initially, to help I would already create the variable $ w_query with a value of 'where 1 = 1' (pog, but it helps a lot) and it would look something like this, I did not test and I do not know if It will work out!

$w_query = "where 1=1"

if($w_op == "P"){ //P é o periférico
    $w_query .= " AND sai_tb_peri.seq_peri = '$w_cb_peri'";
}

if($w_op == "N"){ //N é a nota fiscal
    $w_query .= " AND sai_cad_nf.num_nf = '$w_tx_nota'";
}

$w_query .= " AND sai_cad_nf.dt_nota_fisc between '$w_tx_dt_inic' and '$w_tx_dt_fina'";


$w_querybusca="SELECT * FROM sai_cad_cara_peri
                    INNER JOIN sai_tb_peri on sai_cad_cara_peri.fk_seq_peri = sai_tb_peri.seq_peri
                    INNER JOIN sai_cad_nf ON sai_cad_cara_peri.fk_seq_nf = sai_cad_nf.seq_nf";
$w_querybusca.= $w_query;
    
05.11.2014 / 13:48
0

Rewriting your code, switch usage is more advisable than using if..else , at all times that you can replace your if with switch , do so.

switch ($w_op) 
{
case "P":
    $w_where = "where sai_tb_peri.seq_peri = '$w_cb_peri'";   
    break;
case "N":
    $w_where = "where sai_cad_nf.num_nf = '$w_tx_nota'";
    break;
default:
    $w_where = "where sai_cad_nf.dt_nota_fisc between '$w_tx_dt_inic' and '$w_tx_dt_fina'";
}

$w_query = "SELECT * FROM sai_cad_cara_peri ".
           "INNER JOIN sai_tb_peri on sai_cad_cara_peri.fk_seq_peri = sai_tb_peri.seq_peri ".
           "INNER JOIN sai_cad_nf ON sai_cad_cara_peri.fk_seq_nf = sai_cad_nf.seq_nf ";
$w_query .= $w_where;

To help you with the query itself, or even the structure, I believe your question could be complemented with more information about the bank, receiving incoming information, etc. .

A suggestion: Do not mount complex query in PHP , opt to mount a VIEW to do the join and in your application the code will be cleaner and easier of work. Each one with its responsibilities, the bank can also (and should) have a more worked structure, views , procedures , triggers , functions .     

05.11.2014 / 14:11