$searchParams is actually an array which -if all the options are turned on looks like this
[q] => 77 [enhanced] => Y [no_subjects] => Y [visible] => Y
the user can change it so that the enhanced option might or might not be turned on….etc…
depending on that the look of the array will change
[q] => 77 [enhanced] => N [no_subjects] => N [visible] => N
etc…
public function getWebBookBasicSearchByParams($searchParams){
$q = $searchParams['q'];
$fields = "`bw`.`id`,`bw`.`isbn`,`bw`.`book_title`,`bw`.`edited_by`,`bw`.`book_schedule_date`,`bw`.`downloaded_num`,`bw`.`visible_online`,`snr`.`subject_id`";
$join= "LEFT JOIN {$this->_t_subjects_relations} snr ON (bw.id = snr.object_id and snr.type ='bookweb') ";
$counter=0;
$add="";
if (isset($searchParams['q']) && !empty($searchParams['q'])) {
if($searchParams['no_subjects'] == 'Y'){
$add.= " snr.subject_id = NULL";
$counter ++;
}
if($searchParams['enhanced'] == 'Y'){
$add.= " MATCH(bw.description1,bw.about_the_book) AGAINST ('{$q}'IN BOOLEAN MODE)";
$counter++;
}
if($searchParams['visible'] == 'Y'){
$add.= " bw.visible_online = 1";
$counter++;
}
if($searchParams['visible'] == 'N'){
$add.= " bw.visible_online = 0";
$counter++;
}
$inCopula = ($counter > 0) ? " AND" : " " ;
$extCopula = ($counter > 0) ? "WHERE " : " " ;
}
$query = "SELECT {$fields} FROM {$this->_t_books_web} bw {$join}{$extCopula}{$add}";
with this code I get this $query
SELECT `bw`.`id`,`bw`.`isbn`,`bw`.`book_title`,`bw`.`edited_by`,`bw`.`book_schedule_date`,`bw`.`downloaded_num`,`bw`.`visible_online`,`snr`.`subject_id` FROM `books_web` bw LEFT JOIN `subjects_new_relations` snr ON (bw.id = snr.object_id and snr.type ='bookweb')
AND snr.subject_id = NULL
MATCH(bw.description1,bw.about_the_book) AGAINST ('77'IN BOOLEAN MODE) bw.visible_online = 1
obviously it is wrong, and it should look like this
SELECT `bw`.`id`,`bw`.`isbn`,`bw`.`book_title`,`bw`.`edited_by`,`bw`.`book_schedule_date`,`bw`.`downloaded_num`,`bw`.`visible_online`,`snr`.`subject_id` FROM `books_web` bw LEFT JOIN `subjects_new_relations` snr ON (bw.id = snr.object_id and snr.type ='bookweb')
**WHERE**(snr.subject_id = NULL **AND**
MATCH(bw.description1,bw.about_the_book) AGAINST ('77'IN BOOLEAN MODE) **AND** bw.visible_online = 1)
How would you modify the code above in order to get the desired mysql query, thank you for your help!
Just use an array to store all your criteria and use
implodeto aggregate them.