dannybrazil Posted July 28, 2009 Share Posted July 28, 2009 Hello im not a pro' but im sure that there is a better way and shorter to get data from my DB according the form (link : Meu-ape) here is the code : //Here we count the number of results //Edit $data to be your query //category $category_sql="category='$category' "; //cidade $cidade_sql="cidade='$cidade'"; //bairro $bairro_sql="bairro='$bairro'"; //bairro_Tudo $bairro_tudo="bairro IN ('Lagoa Nova','Tirol')"; //quartos = Tudo $quartos_tudo="quartos IN ('1','2','3','4','Kitinet')"; //quartos $quartos_sql="quartos='$quartos' "; //preco between $preco_between="preco_between BETWEEN '$preco1' AND '$preco2'"; // now the SQL if($quartos=="Tudo"&&$bairro=="Tudo"&&$preco1==""&&$preco2=="") { $data = mysql_query("SELECT * FROM _Form_Nr_2 WHERE $category_sql AND $cidade_sql AND $bairro_tudo AND $quartos_tudo") or die(mysql_error()); } elseif($quartos=="Tudo"&&$bairro=="Tudo") { $data = mysql_query("SELECT * FROM _Form_Nr_2 WHERE $category_sql AND $cidade_sql AND $bairro_tudo AND $preco_between AND $quartos_tudo") or die(mysql_error()); } elseif($bairro=="Tudo") { $data = mysql_query("SELECT * FROM _Form_Nr_2 WHERE $category_sql AND $cidade_sql AND $bairro_tudo AND $preco_between AND $quartos_sql") or die(mysql_error()); } elseif($quartos=="Tudo") { $data = mysql_query("SELECT * FROM _Form_Nr_2 WHERE $category_sql AND $cidade_sql AND $bairro_sql AND $preco_between AND $quartos_tudo") or die(mysql_error()); } else $data = mysql_query("SELECT * FROM _Form_Nr_2 WHERE $category_sql AND $cidade_sql") or die(mysql_error()); while($row = mysql_fetch_array($data)) { // beginning of row// echo '<tr style=" border-bottom: 1px solid #fff;" onmouseover="mover(this);" onmouseout="mout(this);">'; ... and so on... can i improve or change the if...else if code with something else that will include it all togther Thanks Quote Link to comment https://forums.phpfreaks.com/topic/167837-mysql-advice/ Share on other sites More sharing options...
BLaZuRE Posted July 29, 2009 Share Posted July 29, 2009 Hello im not a pro' but im sure that there is a better way and shorter to get data from my DB according the form (link : Meu-ape) here is the code : //Here we count the number of results //Edit $data to be your query //category $category_sql="category='$category' "; //cidade $cidade_sql="cidade='$cidade'"; //bairro $bairro_sql="bairro='$bairro'"; //bairro_Tudo $bairro_tudo="bairro IN ('Lagoa Nova','Tirol')"; //quartos = Tudo $quartos_tudo="quartos IN ('1','2','3','4','Kitinet')"; //quartos $quartos_sql="quartos='$quartos' "; //preco between $preco_between="preco_between BETWEEN '$preco1' AND '$preco2'"; // now the SQL if($quartos=="Tudo"&&$bairro=="Tudo"&&$preco1==""&&$preco2=="") { $data = mysql_query("SELECT * FROM _Form_Nr_2 WHERE $category_sql AND $cidade_sql AND $bairro_tudo AND $quartos_tudo") or die(mysql_error()); } elseif($quartos=="Tudo"&&$bairro=="Tudo") { $data = mysql_query("SELECT * FROM _Form_Nr_2 WHERE $category_sql AND $cidade_sql AND $bairro_tudo AND $preco_between AND $quartos_tudo") or die(mysql_error()); } elseif($bairro=="Tudo") { $data = mysql_query("SELECT * FROM _Form_Nr_2 WHERE $category_sql AND $cidade_sql AND $bairro_tudo AND $preco_between AND $quartos_sql") or die(mysql_error()); } elseif($quartos=="Tudo") { $data = mysql_query("SELECT * FROM _Form_Nr_2 WHERE $category_sql AND $cidade_sql AND $bairro_sql AND $preco_between AND $quartos_tudo") or die(mysql_error()); } else $data = mysql_query("SELECT * FROM _Form_Nr_2 WHERE $category_sql AND $cidade_sql") or die(mysql_error()); while($row = mysql_fetch_array($data)) { // beginning of row// echo '<tr style=" border-bottom: 1px solid #fff;" onmouseover="mover(this);" onmouseout="mout(this);">'; ... and so on... can i improve or change the if...else if code with something else that will include it all togther Thanks I'm quite curious as to why you're thinking this way: ... else if ($a==1){do this} // one expression else if ($b==1){do this} // one expression else if ($a==1 && $b==1){do this} // two expressions ... You do know about the OR operator (||), right? If you're implementing the same thing, why so many statements? Remember if $c==2, that means it's ONLY looking at $c, it doesn't care about $d, $e, and anything else. Now look at the above code and notice the pattern. You should be able to do it in ONE if statement with two expressions. Then, compare it to your own code. Try to look for patterns in a similar way. Quote Link to comment https://forums.phpfreaks.com/topic/167837-mysql-advice/#findComment-885596 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.