0

I have multiple fields in search form.Every field could be empty. I build query like this:

$search_title = trim($_POST["search_title"]);
$search_skill = trim($_POST["search_skill"]);
$search_company = trim($_POST["search_city"]);
$search_country_id = trim($_POST["search_county_id"]);

$hits = $index->find("title:$search_title and skill:$search_skill and city:$search_city and country_id:$country_id");

User can only fill title or skill or city etc. but if some field is empty i have no result. I have result only if all fields filled and matched. I wont results if only one field is filled,if is null ignore that field:

$hits = $index->find("title: and skill: and city: and country_id:$country_id");    
Goran Radovanovic
  • 103
  • 1
  • 1
  • 9
  • Look at this similar question I answered earlier for ideas: http://stackoverflow.com/questions/9764950/mysql-updating-some-database-fields-without-overwriting-fields-not-changed/9765118#9765118 – Jeremy Harris Mar 19 '12 at 09:57

1 Answers1

0

you might try something like this:

if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost)) {
                //get filtered and valid values from search form.
                //filter the array for set values
                $data = array_filter($form->getValues());
                //extract key => vaules as $variable = values
                extract($data);
                 $query1 = new Zend_Search_Lucene_Search_Query_MultiTerm();
                 //test for variable isset and add term to query
                 if (isset($search_title)){
                     $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_title, 'title'));
                 }
                 if (isset($search_skill)){
                     $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_skill, 'skill'));
                 }
                 if (isset($search_city)){
                     $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_city, 'city'));
                 }
                 if (isset($search_country_id)){
                     $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_country_id, 'country_id'));
                 }
                 //This should give the AND you are looking for...I hope
                 $query= new Zend_Search_Lucene_Search_Query_Boolean(array($query1), array(TRUE));
                 //get result set from query
                 $hits = $index->find($query);
            }
        }

if you use the StringTrim filter in your forms you won't need to use the trim() function on your data. The $_POST array is dangerous with user supplied data, ZF provides the getValues() series of methods to provide data from the Request Object (POST and GET) the have had filters and validators you specify applied.
I used the extract() function in this instance because I used getValues() so the data has been filtered and validated. There are of course other valid ways to assign key => value pairs to variables. Use your favorite.

RockyFord
  • 8,529
  • 1
  • 15
  • 21