开发者

PHP multi criteria search using checkboxes

I am trying to make a site for my real estate business. Until now i have managed to make correct queries using dropdown menus that select prices,sqm,bedrooms, etc

Now i am trying to make a multi-search criteria where a user can choose the kind of appartments to find..like studio,penthouse,single_floor appartment and etc via multi checkboxes. In my database i have 8 columns that correspnd to the kinds of houses and each one of them can take a boolean value. The columns are named OB_STUDIO,OB_PENTHOUSE,..... What i have done is create as many chkboxes as the categories are

<input type="che开发者_运维知识库ckbox" name="chk1" value="studio" />
<input type="checkbox" name="chk2" value="penthouse" />

. . Then in my php code i wrote....

if ($chk1 == 'studio') {
$where .= " and A.OB_STUDIO= true ";
}
if ($ch2 == 'penthouse') {
$where .= " and A.OB_PENTHOUSE= true ";
}

Then i pass the query string as a variable

$request_string = "from " . G_DB_PREFIX . "OBJECTS A, 
" . G_DB_PREFIX . "OBJECT_TR B, 
" . G_DB_PREFIX . "TRANSLATION C
where A.OB_ID=B.OTR_OBJECTID and B.OTR_LANG='" . G_LANG . "'
and A.OB_PRODUCTID=C.TR_REFID and C.TR_LANG='" . G_LANG . "' and
C.TR_REFNAME='PRODUCTID' $where";

But instead of turning back all the houses that belong to those two categories (in case we check both) turns nothing. I understand that the reason is because the query as it is searches for products that belong to both categories. What kind of expression do i have to use to achieve what i want? Probably i have to use a checkbox array for that? The code works just fine if i only check one category. I am really newbie to PHP so if you can give me a hand i will be grateful. Thanx in advance


Try this:

$conditions = array();
if ($chk1 == 'studio') {
$conditions[] = 'A.OB_STUDIO= true';
}
if ($chk2 == 'penthouse') {
$conditions[] = 'A.OB_PENTHOUSE= true';
}

$where = !empty($conditions) ? ' and ('.implode(' or ',$conditions).') ' : '';


Your query returns nothing because you ask for all the houses which belongs to these two categories in the same time. If you need houses from either category you should query:

$where .= " and (A.OB_STUDIO= true or A.OB_PENTHOUSE= true)";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜