my $hash ={ Initiative=>'DriveInitiative', ResOrientation => 'DriveResOrient'}; my @clauses = (); foreach my $checkbox (keys %$hash) { push @clauses, $hash->{$checkbox} if validate($cgi->param($checkbox)); } my $where_clause = join(' and ',map($_.'= 'on', @clauses)); my $sql = "select a.ResType, a.ResLevel, a.ResManage a.Details, a.Length, a.Source, a.Cost, a.FurtherDetails from Resources a INNER JOIN ResourceSettings b ON a.ResID = b.ResID"; $sql .= " where $where_clause " if ($where_clause); $sql .= " order by a.ResType, a.ResLevel, a.ResManage, a.Details, a.Length, a.Source, a.Cost, a.FurtherDetails";