Jump to content

mysql advice


dannybrazil

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/167837-mysql-advice/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/167837-mysql-advice/#findComment-885596
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.