Jump to content

Archived

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

Sallie_ann

Select, Where, If, Else Statement Question

Recommended Posts

Hi,

 

I am trying to create a select statement that will extract data from a MySQL database when the user selects a number of options from 4 drop down list boxes.

 

Here is my code for $where1 (there are 4 in total).

 

 

switch($_POST[\'company_size\'])

{

case 1:

$where1 = \"company_size LIKE \'Small\'\";

break;

case 2:

$where1 = \"company_size LIKE \'Medium\'\";

break;

case 3:

$where1 = \"company_size LIKE \'Large\'\";

break;

default:

break;

}

 

 

This all works fine but I am not sure how to construct the where clause so that only one or up to all four options can be selected from the options.

 

 

$whereclause = $where1;

 

if($whereclause == \"\")

{

$whereclause = $where2;

}

else

{

if($whereclause2 == \"\")

{

$whereclause = $where3;

}

else

{

if($whereclause3 == \"\")

{

$whereclause = $where4;

}

else

{

 

if($where4 != \"\")

{

$whereclause .= \" AND \" . $where2 .= \" AND \" . $where3 .= \" AND \" . $where4;

}

}

 

if($whereclause != \"\")

{

$whereclause = \" WHERE \" . $whereclause;

}

 

$sql = \"SELECT name, County, sector, company_size

FROM beacon

$whereclause\";

 

 

Anybody able to help?!

Share this post


Link to post
Share on other sites

Try something like...

 

[php:1:857bb6e23d]switch($_POST[\'company_size\'])

{

case 1:

$where1 = \"Small\";

break;

case 2:

$where1 = \"Medium\";

break;

case 3:

$where1 = \"Large\";

break;

default:

break;

}

 

$query = \"SELECT name, County, sector, company_size FROM beacon WHERE itemhere LIKE $where1\";[/php:1:857bb6e23d]

Share this post


Link to post
Share on other sites

×

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.