Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.