Reputation: 175
UPDATE
I've improved my code to this:
$query_init = "SELECT * FROM data WHERE 1=1";
24 // $_POST to PHP var.
25 $t_bebida = $_POST['t_bebida'];
26 $t_uf = $_POST['t_UF'];
27 $t_canal = $_POST['t_canal'];
28 $t_ano = $_POST['t_ano'];
29
30 if ($t_bebida != "Todos") {
31 $query_final .= " AND BEBIDA = '$t_bebida'";};
32
33 if ($t_ano != "Todos") {
34 $query_final .= " AND ANO = '$t_ano'";};
35
36 if ($t_uf != "Todos") {
37 $query_final .= " AND UF = '$t_uf'";};
38
39 if ($t_canal != "Todos") {
40 $query_final .= " AND CANAL = '$t_canal'";};
41
42 $query = $query_init . $query_final;
But when I'm receiving "todos" for t_bebida, I get this error:
Notice: Undefined variable: query_final in /*/ on line 36
Any help?
Short time ago, I've asked a question about treatment of $_POST.
But there they answered me that I need to reformulate my query and do some IFs to query run properly.
I have this situation:
A user will send by $_POST this vars:
t_bebida
t_uf
t_canal
t_ano
Each one of these vars can come with a value of "Todos".
To exemplify the situation, this is my code:
$query_init = "SELECT * FROM data WHERE";
// $_POST to PHP var.
$t_bebida = $_POST['t_bebida'];
$t_uf = $_POST['t_UF'];
$t_canal = $_POST['t_canal'];
$t_ano = $_POST['t_ano'];
//IF BEBIDA == todos
if ($t_bebida == "Todos") {
$query_final = " UF = '$t_uf' AND CANAL = '$t_canal' AND ANO = '$t_ano' ORDER BY BEBIDA ASC";}
//IF UF == todos
if ($t_uf == "Todos") {
$query_final = " BEBIDA = '$t_bebida' AND UF = '$t_uf' AND CANAL = '$t_canal' AND ANO = '$t_ano'";}
//IF CANAL ==todos
if ($t_canal == "Todos") {
$query_final = " BEBIDA = '$t_bebida' AND UF = '$t_uf' AND ANO = '$t_ano'";}
//IF ANO == todos
if ($t_canal == "Todos") {
$query_final = " BEBIDA = '$t_bebida' AND UF = '$t_uf' AND CANAL = '$t_canal'";}
//IF BEBIDA + UF == todos
if ($t_bebida == "Todos" and $t_uf == "Todos") {
$query_final = " CANAL = '$t_canal' AND ANO = '$t_ano'";}
//IF BEBIDA + UF + CANAL == todos
if ($t_bebida == "Todos" and $t_uf == "Todos" and $t_canal == "Todos") {
$query_final = " ANO = '$t_ano'";}
//IF BEBIDA + UF + ANO == todos
if ($t_bebida == "Todos" and $t_uf == "Todos" and $t_ano == "Todos") {
$query_final = " CANAL = '$t_canal'";}
//IF BEBIDA + CANAL == todos
if ($t_bebida == "Todos" and $t_canal == "Todos"){
$query_final = " UF = '$t_uf' AND ANO = '$t_ano'";}
//IF BEBIDA + CANAL + ANO == todos
if ($t_bebida == "Todos" and $t_canal == "Todos" and $t_ano == "Todos"){
$query_final = " UF = '$t_uf'";}
//IF BEBIDA + ANO == todos
if ($t_bebida == "Todos" and $t_ano == "Todos"){
$query_final = " UF = '$t_uf' AND CANAL = '$t_canal'";}
//IF UF + CANAL == todos
if ($t_uf == "Todos" and $t_canal == "Todos"){
$query_final = " BEBIDA = '$t_bebida' AND ANO = '$t_ano'";}
//IF UF + ANO == todos
if ($t_uf == "Todos" and $t_ano == "Todos"){
$query_final = " BEBIDA = '$t_bebida' AND CANAL = '$t_canal'";}
//IF UF + CANAL + ANO == todos
if ($t_uf == "Todos" and $t_canal == "Todos" and $t_ano == "Todos"){
$query_final = " BEBIDA = '$t_bebida'";}
//IF CANAL + ANO == todos
if ($t_uf == "Todos" and $t_ano == "Todos"){
$query_final = " BEBIDA = '$t_bebida' AND CANAL = '$t_canal'";}
//IF NONE == todos
if ($t_bebida != "Todos" and $t_uf != "Todos" and $t_ano != "Todos" and $t_UF != "Todos" and $t_canal != "Todos"){
$query_final = " BEBIDA = '$t_bebida' AND UF = '$t_uf' AND CANAL = '$t_canal' AND ANO = '$t_ano'";}
// Build-UP query
$query = $query_init . $query_final;
This is not working for me, It works only for 't_bebida == todos' or 'none == todos'.
So, what I'm facing is that I'm not correctly structuring IFs to the query.
Can someone help me to figure it out?...
Thanks,
Upvotes: 0
Views: 100
Reputation: 15464
Translation of Todos.
Better to rewrite your code. And use this feature.
$query_init = "SELECT * FROM data WHERE 1=1";
$query_final = '';
if ($t_bebida != "Todos")
$query_final .= " AND BEBIDA = '$t_bebida'";
if($t_ano != "Todos")
$query_final .= " AND ANO = '$t_ano'";
if ($t_uf != "Todos")
$query_final .= " AND UF = '$t_uf'";
if ( $t_canal != "Todos")
$query_final .= " AND CANAL = '$t_canal'";
$query = $query_init . $query_final;
Also do not forget to use prepared statements.
Upvotes: 1
Reputation: 2492
This is just to show how your if's can be enhanced . You should better look into using PDO or MySQLi .
$query = "SELECT * FROM `data` WHERE 1 ";
$t_bebida = $_POST['t_bebida'];
$t_uf = $_POST['t_UF']; // Is this t_UF or t_uf ?
$t_canal = $_POST['t_canal'];
$t_ano = $_POST['t_ano'];
if( $t_bebida <> "Todos" )
{
$query .= " AND `BEBIDA` = '$t_bebida'";
$order_by_column = "`BEBIDA`";
}
if( $t_uf <> "Todos" )
{
$query .= " AND `UF` = '$t_uf'";
$order_by_column = "`UF`";
}
if( $t_canal <> "Todos" )
{
$query .= " AND `CANAL` = '$t_canal'";
$order_by_column = "`CANAL`";
}
if( $t_ano <> "Todos" )
{
$query .= " AND `ANO` = '$t_ano'";
$order_by_column = "`ANO`";
}
$query .= " ORDER BY " .$order_by_column ." ASC"; // LIMIT 100
echo query;
Upvotes: 1