We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46876
    • 23 Posts
    Hello again, getting stumped over this small issue.

    Basically I'm using migxLoopCollection within getPage to create a very simple search engine.

    Users search the entries based on the entry title or keywords associated with the entry. They may choose to search through all or specific sources.

    This is the code I am using:

    [[!getPage@articlePaging?
    &element=`migxLoopCollection`
    &packageName=`redaspackage`
    &classname=`redasPackage`
    &tpl=`articleResults`
    &limit=`20`
    &where=`{"title:LIKE":"[[!GetSearchTerm]]",
    "OR:keywords:LIKE":"[[!GetSearchTerm]]",
    "resource_id:LIKE":"[[!GetSource]]"}`
    &sortConfig=`[{"sortby":"createdon","sortdir":"DESC"}]`]]
    


    The snippets are used to get the values that are passed through the URL. [[!GetSource]] returns a wildcard (%) when no source is chosen.

    All this is working, however, when searching for entries in a specific source, it displays all the entries from ALL sources that contain the search terms. This is not an issue when I remove the 'OR' clause ("OR:keywords:LIKE").

    Any help would be greatly appreciated, thank you!

    This question has been answered by Bruno17. See the first response.

    • discuss.answer
      • 4172
      • 5,888 Posts
      try something like that:

      snippet preparewhere:

      $term = $modx->getOption('term',$_REQUEST,'');
      $source = $modx->getOption('source',$_REQUEST,'');
      
      $wheres = array();
      
      if (!empty($source)){
          $wheres[] = array('resource_id'=>$source);
      }
      
      if (!empty($term)){
          $wheres[] = array('title:LIKE'=>'%'.$term.'%','OR:keywords:LIKE'=>'%'.$term.'%');
      }
      
      return $modx->toJson($wheres);


      [[!getPage@articlePaging?
      &element=`migxLoopCollection`
      &packageName=`redaspackage`
      &classname=`redasPackage`
      &tpl=`articleResults`
      &limit=`20`
      &where=`[[!preparewhere]]`
      &sortConfig=`[{"sortby":"createdon","sortdir":"DESC"}]`]]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 46876
        • 23 Posts
        It works like a charm, thanks Bruno!