Jump to content

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?!

Link to comment
https://forums.phpfreaks.com/topic/700-select-where-if-else-statement-question/
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]

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.