Hvis du vil beholde det nuværende design med visning gemt i en enkelt celle, så er der noget, du kan gøre. Selvom jeg ikke foreslår det, giver jeg et eksempel nedenfor, fordi du allerede har designet dit projekt som dette.
BEMÆRKNING: Det er et testeksempel for at se den grundlæggende funktionalitet. Dette er ikke den endelige kode, fordi sql-injektion og andre ting tages ikke i betragtning.
Jeg antager, at visninger er gemt i en enkelt celle, afgrænset mellemrum, og hvis en visning består af mere end ét ord - er de placeret imellem, for eksempel City-Center.
Studer dette eksempel og se, om du kan tilpasse det til dine behov:
<?PHP
echo '<pre>';
//mysql connect
mysql_connect('localhost', 'root','');
mysql_select_db("test");
//add some tsting data
addTestingData();
//build sql from user input via $_GET
$sqlConditions = builtSql();//build sql conditions
$sql = 'select * from `building` where '.$sqlConditions;//build final sql
//get data from mysql
$result = mysql_query($sql ) ;
while($row= mysql_fetch_row($result) )
print_r( $row );
///////////////end////////////////////////////////////////////
function addTestingData()
{
mysql_query("DROP TABLE IF EXISTS `Building`");
mysql_query("
CREATE TABLE `Building` (
`building_uniqueid` MEDIUMINT UNSIGNED NOT NULL ,
`building_street` VARCHAR(30) NOT NULL,
`building_street_nr` VARCHAR(7) NOT NULL,
`building_neighborhood` VARCHAR(30) NOT NULL,
`building_view` VARCHAR(250) NOT NULL,
`building_condition` VARCHAR(150) NOT NULL,
`building_frontage` VARCHAR(30) NOT NULL,
`building_listprice` float NOT NULL,
`building_year` smallint not null,
`bsnss_comments` VARCHAR(255),
PRIMARY KEY (`building_uniqueid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
");
mysql_query('
insert into `building` (`building_uniqueid`,`building_street`,`building_street_nr`,
`building_neighborhood`,`building_view`,`building_condition`,`building_frontage`,
`building_listprice`,`building_year`,`bsnss_comments`) values
("1","street1","strnr1","neighb1","Mountain Ocean Lake Park City-Center",
"good","frontage1","500.3","1990","good building")
');
mysql_query('
insert into `building` (`building_uniqueid`,`building_street`,`building_street_nr`,
`building_neighborhood`,`building_view`,`building_condition`,`building_frontage`,
`building_listprice`,`building_year`,`bsnss_comments`) values
("id2","street1","strnr1","neighb2","River Ocean Lake Park City-Center",
"very good","frontage1","800.5","1991","good building")
') or die(mysql_error());
mysql_query('
insert into `building` (`building_uniqueid`,`building_street`,`building_street_nr`,
`building_neighborhood`,`building_view`,`building_condition`,`building_frontage`,
`building_listprice`,`building_year`,`bsnss_comments`) values
("3","street3","strnr3","neighb1","Ocean Park City-Center",
"fantastic","frontage77","600.7","1994","good building")
');
mysql_query('
insert into `building` (`building_uniqueid`,`building_street`,`building_street_nr`,
`building_neighborhood`,`building_view`,`building_condition`,`building_frontage`,
`building_listprice`,`building_year`,`bsnss_comments`) values
("4","street4","strnr4","neighb1","Ocean Park Mountain City-Center",
"good","frontage1","500.23","1994","good")
');
$_GET['Neighborhood']=array('neighb1');
$_GET['View']=Array('Mountain','River', 'City Center');
$_GET['Condition']=array('good','very good');
$_GET['Frontage']=array('frontage77','frontage1');
$_GET['minPrice']='500';
$_GET['maxPrice']='600';
$_GET['minYear']='1990';
$_GET['maxYear']='1995';
}
function builtSql()
{
$sqlBuild = '( ';
//formate sql for Neighborhood
foreach($_GET['Neighborhood'] as $value)
$sqlBuild .=' `building_neighborhood` = \''.$value.'\' or ';
$sqlBuild=removeLastOr($sqlBuild);
$sqlBuild.=') and (';
//formate sql for View
foreach($_GET['View'] as $value)
$sqlBuild .=' `building_view` LIKE \'%'.str_replace(" ", "-",$value).'%\' or ';
$sqlBuild=removeLastOr($sqlBuild);
$sqlBuild.=') and (';
//formate sql for Condition
foreach($_GET['Condition'] as $value)
$sqlBuild .=' `building_condition` = \''.$value.'\' or ';
$sqlBuild=removeLastOr($sqlBuild);
$sqlBuild.=') and (';
//formate sql for Frontage
foreach($_GET['Frontage'] as $value)
$sqlBuild .=' `building_frontage` = \''.$value.'\' or ';
$sqlBuild=removeLastOr($sqlBuild);
$sqlBuild.=') and (';
//formate sql for Price
$sqlBuild.=
' `building_listprice` BETWEEN \''.$_GET['minPrice'].'\' and \''.$_GET['maxPrice'].'\' ';
$sqlBuild=removeLastOr($sqlBuild);
$sqlBuild.=') and (';
//formate sql for Year
$sqlBuild.=
' `building_year` BETWEEN \''.$_GET['minYear'].'\' and \''.$_GET['maxYear'].'\' ';
$sqlBuild.=') ';
return $sqlBuild;
}
function removeLastOr($str)
{
$tmp=substr($str ,0,(strlen($str )-2));
return $tmp=substr($str ,0,(strlen($str )-3));
}
?>
Selvom du ser nogle foreach-løkker, er der ingen grund til bekymring, da de kører for små arrays, der indeholder brugerdata, så overvej dem til at køre superhurtigt, for der er ingen mysql-forespørgsel involveret!!
Hvis der er et problem, kan du overveje at give detaljer om db-skemaet og en grundlæggende beskrivelse. Håber dette kan hjælpe!