Jump to content


Photo

Select, Where, If, Else Statement Question


  • Please log in to reply
1 reply to this topic

#1 Sallie_ann

Sallie_ann
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 11 July 2003 - 11:42 AM

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

#2 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 11 July 2003 - 09:41 PM

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]
DEFINE("YOU","Spending Too Much Time On The Computer");




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users