Jika Anda ingin mempertahankan desain saat ini dengan tampilan yang disimpan dalam satu sel, maka ada sesuatu yang dapat Anda lakukan. Meskipun saya tidak menyarankannya, saya memberikan contoh di bawah ini karena Anda telah merancang proyek Anda seperti ini.
PERHATIKAN: Ini adalah contoh pengujian, untuk melihat fungsionalitas dasar. Ini bukan kode final karena injeksi sql dan hal-hal lain tidak dipertimbangkan.
Saya berasumsi bahwa tampilan disimpan dalam satu sel, dibatasi ruang, dan jika tampilan terdiri dari lebih dari satu kata - ditempatkan di antaranya, misalnya Pusat Kota.
Pelajari contoh ini dan lihat apakah Anda dapat menyesuaikannya dengan kebutuhan Anda:
<?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));
}
?>
Meskipun Anda melihat beberapa foreach loop, tidak perlu khawatir karena mereka berjalan untuk array kecil yang berisi data pengguna, jadi anggap saja mereka berjalan super cepat karena tidak ada kueri mysql yang terlibat!!
Jika masih ada masalah, pertimbangkan untuk memberikan detail tentang skema db dan beberapa deskripsi dasar. Semoga ini bisa membantu!