Forums

Combining filters in WHERE section

flyboeing 09 Sep, 2011
Hello all,

I am following the how-to-docs of Dynamic Filters 2.
From this I got the idea to make that. This works fine, but I also have a text filter.

How do I combine these two?

This is the code I currently have:
<?php
$title = JRequest::getString('title', '', 'post');
if ( $title ) {
echo " WHERE `dest` LIKE '%$title%'
OR `reg` LIKE '%$title%' 
OR `type` LIKE '%$title%' 
OR `aircraft` LIKE '%$title%' 
OR `callsignin` LIKE '%$title%'
OR `callsignout` LIKE '%$title%'  
OR `orig` LIKE '%$title%'
OR `dest` LIKE '%$title%'
OR `user` LIKE '%$title%'
OR `owner2` LIKE '%$title%'   
OR `remarks` LIKE '%$title%'  
";
}
?>

<?php
$year = JRequest::getInt('year', '0', 'post');
if ( $year ) {
    echo " WHERE `year` = '$year' ";
}
?>


In the how-to-docs there is this code:

<?php
// create the empty array
$where = array();
// check the text filter
$filter_text = JRequest::getString('filter_text', '', 'post');
if ( $filter_text ) {
  $where[] = "( `title` LIKE '%$filter_text%' 
    OR `introtext` LIKE '%$filter_text%' 
    OR `fulltext` LIKE '%$filter_text%' )";
}
// check the catid filter
$filter_catid =& JRequest::getVar('filter_catid', array(), 'post', 'array');
if ( count($filter_catid) ) {
  $filter = implode(', ', $filter_catid);
  $where[] = " `catid` IN ($filter) ";
}
if ( count($where) ) {
  $where = implode(' AND ', $where);
  echo " WHERE $where ";
}
?>


The first part I understand, but how do I make the second part of it?
GreyHead 16 Sep, 2011
Hi flyboeing,

Please try:
<?php
$title = JRequest::getString('title', '', 'post');
$where = array();
if ( $title ) {
  $where[] = "( `dest` LIKE '%$title%'
    OR `reg` LIKE '%$title%'
    OR `type` LIKE '%$title%'
    OR `aircraft` LIKE '%$title%'
    OR `callsignin` LIKE '%$title%'
    OR `callsignout` LIKE '%$title%' 
    OR `orig` LIKE '%$title%'
    OR `dest` LIKE '%$title%'
    OR `user` LIKE '%$title%'
    OR `owner2` LIKE '%$title%'   
    OR `remarks` LIKE '%$title%' ) ";
}
$year = JRequest::getInt('year', '0', 'post');
if ( $year ) {
    $where[] = " `year` = '$year' ";
}
if ( count($where) ) {
  echo " WHERE ".implode(' AND ', $where);
}
?>

Bob
flyboeing 19 Sep, 2011
Hello Bob,

Thank you for your reply and help.

I tried the piece of code you posted and it is partly working.
I do not get the error if I use them both, but..

When I first select the year it shows all the results with that year. But if I want to specify my search using the textbox, it won't work.

When I use the textbox first I get the error message (there are no results), but when I then use the drop down menu with the years and select one, it shows all the results using the searchtext and the selected year.

For example:
If I first select the year 2010, I will get the results where the year is 2010. In my case you get Corendon Airlines and Transavia Airlines. Now I only want to show the Transavia Airlines. I use the textbox and type "Transavia" (without ""). It won't show anything (just my error message: "No results found".

If I first type "Transavia" and submit my search, it shows my error message. Then I select my year and is shows the results of transavia in 2010.

How can I make it possible to show all the results when I use the textbox and when I first select the year and then the textbox?
GreyHead 20 Sep, 2011
Hi flyboeing,

As far as I an see the code *should* do what you want. If it isn't then either there's a typo somewhere or we’ve got the logic wrong.

Try adding an echo line to output the value of the WHERE expression. I think it should be one of the following [list=a]
  • WHERE ( `dest` LIKE '%Transavia%' . . . OR `remarks` LIKE '%Transavia%' )

  • WHERE `year` = '2010'

  • WHERE ( `dest` LIKE '%Transavia%' . . . OR `remarks` LIKE '%Transavia%' ) AND `year` = '2010'

  • blank
  • [/list:o]
    Bob
    This topic is locked and no more replies can be posted.