Als u het huidige ontwerp met weergave in één cel wilt bewaren, dan kunt u iets doen. Hoewel ik het niet suggereer, geef ik hieronder een voorbeeld omdat je je project al zo hebt ontworpen.
OPMERKING: Het is een testvoorbeeld, om de basisfunctionaliteit te zien. Dit is geen definitieve code, want sql-injectie en andere dingen worden niet in overweging genomen.
Ik ga ervan uit dat weergaven worden opgeslagen in een enkele cel, door spaties gescheiden, en als een weergave uit meer dan één woord bestaat, ertussen worden geplaatst, bijvoorbeeld Stadscentrum.
Bestudeer dit voorbeeld en kijk of u het aan uw behoeften kunt aanpassen:
<?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));
}
?>
Hoewel je een aantal foreach-lussen ziet, hoef je je geen zorgen te maken, want ze draaien voor kleine arrays die gebruikersgegevens bevatten, dus beschouw ze als supersnel omdat er geen mysql-query bij betrokken is!!
Als er een probleem blijft, overweeg dan om details te geven over het db-schema en een basisbeschrijving. Ik hoop dat dit kan helpen!