Search problem
16 posts by 2 authors in: Forums > CMS Builder
Last Post: August 27, 2012 (RSS)
By honolulu - July 31, 2012
I have table for Real Estate properties called 'imoti':
Search index code head:
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
require_once "/some-path/lib/viewer_functions.php";
list($imotiRecords, $imotiMetaData) = getRecords(array(
'tableName' => 'imoti',
));
?>
Body form code:
<form name="search" method="GET" action="result-nov-search.php">
<select name="oblast_grad[]" multiple="multiple" size="5">
<option value="Трошево">Трошево</option>
<option value="Младост">Младост</option>
<option value="Възраждане">Възраждане</option>
<option value="Владислав Варненчик">Владислав Варненчик</option>
<option value="Кайсиева градина">Кайсиева градина</option>
</select>
<input type="submit" name="submit" value="Search" />
</form>
The result page - result-nov-search.php:
Head:
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
require_once "/some-path/lib/viewer_functions.php";
list($imotiRecords, $imotiMetaData) = getRecords(array(
'tableName' => 'imoti',
'orderBy' => mysql_escape(@$_REQUEST['orderBy']),
'perPage' => '2',
));
?>
List:
<h1>Real Estate - List Page Viewer</h1>
<?php foreach ($imotiRecords as $record): ?>
Record Number: <?php echo $record['num'] ?><br/>
Name: <?php echo $record['name'] ?><br/>
Price: <?php echo $record['price'] ?><br/>
State: <?php echo $record['oblast_grad'] ?><br/>
_link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_link'] ?></a><br/>
<hr/>
<?php endforeach ?>
<?php if (!$imotiRecords): ?>
No records were found!<br/><br/>
<?php endif ?>
The thing is when choose 1 options (Трошево) from oblast_grad select, it shows all listing, not only form Трошево for example. When you choose 2 options it is the same - all listings. Is my code wrong or ... ?
In URL - result-nov-search.php?oblast_grad[]=Трошево
The result page have to show the listings form 1 option if the user select 1 or 2,3 options at the same time if he search for properties in 2,3 states.
Thanks for help in advanced :)
Cheers
Re: [honolulu] Search problem
By Jason - July 31, 2012
The issue seems to be that CMS Builder doesn't automatically do a search if the incoming field is an array. In order to do this, you'll need to manually create your own WHERE clause.
What type of field is "oblast_grad". If it's a list, is it single value or multi value list?
Let me know and I'll put together an example for you.
Thanks,
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Search problem
By honolulu - July 31, 2012
Re: [honolulu] Search problem
By Jason - August 1, 2012
You can create a WHERE clause like this:
EXAMPLE:
$where = "";
if (is_array(@$_REQUEST['oblast_grad'])) {
foreach ($_REQUEST['oblast_grad'] as $oblast_grad) {
$where .= " oblast_grad LIKE '%\t".mysql_escape($oblast_grad)."\t%' OR";
}
$where = rtrim($where, "OR");
}
list($imotiRecords, $imotiMetaData) = getRecords(array(
'tableName' => 'imoti',
'orderBy' => mysql_escape(@$_REQUEST['orderBy']),
'perPage' => '2',
'where' => $where,
));
Hope this helps get you started
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Search problem
By honolulu - August 3, 2012
Cheers
Re: [honolulu] Search problem
By Jason - August 3, 2012
Basically you can replicate the code in the above post and modify it to search for other fields. The most important thing to remember about multi value fields in CMS Builder is that they are stored as a string of values, separated by tab characters (\t). That's why you see the tab characters appearing in the where clause.
Give this a try with another field, and let me know if you run into any other problems.
Thanks,
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Search problem
By honolulu - August 4, 2012
i try this, but it does not work:
two fields(multi values)
1 - oblast_grad
2 - vid_na_imota
$where = "";
if (is_array(@$_REQUEST['oblast_grad'])) {
foreach ($_REQUEST['oblast_grad'] as $oblast_grad) {
$where .= " oblast_grad LIKE '%\t".mysql_escape($oblast_grad)."\t%' OR";
}
$where = rtrim($where, "OR");
}
if (is_array(@$_REQUEST['vid_na_imota'])) {
foreach ($_REQUEST['vid_na_imota'] as $vid_na_imota) {
$where .= " vid_na_imota LIKE '%\t".mysql_escape($vid_na_imota)."\t%' OR";
}
$where = rtrim($where, "OR");
}
list($imotiRecords, $imotiMetaData) = getRecords(array(
'tableName' => 'imoti',
'orderBy' => mysql_escape(@$_REQUEST['orderBy']),
'perPage' => '2',
'where' => $where,
));
and search form:
<div style="margin-left:20px; margin-right:20px; position:relative;">
<form id="search" name="search" method="GET" action="results.php">
<div style="float:left; width:300px; margin-right:20px;">
<div>Вид на имота</div><div><select id="state" name="vid_na_imota[]" size="5" multiple="true">
<option value="1-стаен" selected="selected">1-стаен</option>
<option value="2-стаен" selected="selected">2-стаен</option>
<option value="3-стаен">3-стаен</option>
<option value="4-стаен">4-стаен</option>
<option value="Многостаен" selected="selected">Многостаен</option>
</select></div></div>
<div style="float:left; width:300px; margin-right:20px;">
<div>Квартал/Район</div>
<div><select id="city" name="oblast_grad[]" multiple="multiple" size="5">
<optgroup label="Трошево, Младост, Възраждане">
<option value="Трошево">Трошево</option>
<option value="Младост">Младост</option>
<option value="Възраждане">Възраждане</option>
</optgroup>
<optgroup label="Владислав Варненчик, Кайсиева градина">
<option value="option4">Владислав Варненчик</option>
<option value="option5">Кайсиева градина</option>
</optgroup>
</select>
</div>
</div>
<div style="float:left; width:300px; margin-right:20px;"><input type="submit" name="submit" value="Search" /></div>
</form></div>
when chooce 1 option from vid_na_imota and 1 option from oblast_grad it does not do the search correct. Maybe my code is wrong :(
Re: [honolulu] Search problem
By Jason - August 6, 2012
What you'll need to do is group your search elements with parentheses, putting an AND in between.
Try this:
NOTE: This example code has not been tested
$where = "";
if (is_array(@$_REQUEST['oblast_grad'])) {
$where .= "(";
foreach ($_REQUEST['oblast_grad'] as $oblast_grad) {
$where .= " oblast_grad LIKE '%\t".mysql_escape($oblast_grad)."\t%' OR";
}
$where = rtrim($where, "OR") . ")";
}
if (is_array(@$_REQUEST['vid_na_imota'])) {
if ($where) { $where .= " AND "; }
$where .= "(";
foreach ($_REQUEST['vid_na_imota'] as $vid_na_imota) {
$where .= " vid_na_imota LIKE '%\t".mysql_escape($vid_na_imota)."\t%' OR";
}
$where = rtrim($where, "OR") .")";
}
list($imotiRecords, $imotiMetaData) = getRecords(array(
'tableName' => 'imoti',
'orderBy' => mysql_escape(@$_REQUEST['orderBy']),
'perPage' => '2',
'where' => $where,
)); $where = "";
if (is_array(@$_REQUEST['oblast_grad'])) {
foreach ($_REQUEST['oblast_grad'] as $oblast_grad) {
$where .= " oblast_grad LIKE '%\t".mysql_escape($oblast_grad)."\t%' OR";
}
$where = rtrim($where, "OR");
}
if (is_array(@$_REQUEST['vid_na_imota'])) {
foreach ($_REQUEST['vid_na_imota'] as $vid_na_imota) {
$where .= " vid_na_imota LIKE '%\t".mysql_escape($vid_na_imota)."\t%' OR";
}
$where = rtrim($where, "OR");
}
list($imotiRecords, $imotiMetaData) = getRecords(array(
'tableName' => 'imoti',
'orderBy' => mysql_escape(@$_REQUEST['orderBy']),
'perPage' => '2',
'where' => $where,
));
Hope that helps get you started
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Search problem
By honolulu - August 6, 2012
i try your code, but i doesn`t work it show some:
MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'vid_na_imota LIKE '% 1-Ñтаен %' ) LIMIT 2' at line 3.
This search thing is very important for the site, because it is based ot this search to find property that match their criteria for example from 2 different areas by 2 different types of homes in one town.
Cheers