Cannot get OR part of SELECT statement to work in findRecords

This topic is: not resolved

This topic contains 3 replies, has 2 voices, and was last updated by  sc0ttkclark 2 years, 1 month ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #165542

    macnikki
    Member
    Post count: 1

    I have spent several days looking through the forums on this and trying out multiple different methods but nothing is working!

    I have a list of businesses in my pod DIRECTORY. I want to display them all (working) and to provide a search form at the top of the screen for users to filter the list by the primary category and/or a string of text (the form is there and looks perfect). When the list appears I want to have it paginate and only show 5 records per page.

    This is what I tried the first time.

    if (isset($_GET["search"]))
    {
    >$result = addslashes($_GET["search"]);
    }
    $pods = new Pod(‘directory’);
    $pods->getFilters(‘primary_category’,'Search’);

    $pods->findRecords(‘t.name ASC’, 5, "t.name LIKE ‘%$result%’ OR t.description LIKE ‘%$result%’");
    echo $pods->getPagination();
    echo $pods->showTemplate(‘directory_list’);

    When I put in a word such as "adventure" in to the search field it finds no records even though I know there is a record there with that word in the description field. So, I did a dump of the SQL and found this:

    string(530) " SELECT SQL_CALC_FOUND_ROWS DISTINCT t.*, p.id AS pod_id, p.created, p.modified FROM @wp_pod p INNER JOIN

    t ON t.id = p.tbl_row_id WHERE p.datatype = 6 AND (t.name LIKE ‘%adventure%’) AND ( t.name LIKE ‘%adventure%’ OR t.description LIKE ‘%adventure%’ ) ORDER BY t.name ASC LIMIT 0,5 "

    As you can see it is adding in the following to the code: (t.name LIKE ‘%adventure%’) AND

    Because of this it is not finding records that only have ADVENTURE in the description field and not in the name field. So something in the search code is adding in this when the search is executed.

    So, I took a different tack and tried writing my own SQL statement to substitute for the standard findRecords parameters. I wrote this:

    $query=(SELECT * FROM wp_pod_tbl_directory WHERE name LIKE "%$result%" OR description LIKE "%$result");
    $pods->findRecords(, 5, $query);

    This worked great but it ignored the ’5′ for the number of records to display on each page. So, I removed the "5" from the findRecords statement and instead put it in to the $query like this:

    $query=(SELECT * FROM wp_pod_tbl_directory WHERE name LIKE "%$result%" OR description LIKE "%$result" LIMIT 1,5);
    $pods->findRecords(, , $query);

    This time it looked like pagination had worked but actually all records were printed out on the first page, but the where statements were correctly read.

    I have read through posts of other people having similar problems and have tried all the possible combinations that they used of parantheses placement, double quotes, single quotes etc, but nothing seems to work. It all seems to be a problem because the Search code that is being triggered when I carry out the search is adding in this extra (t.name LIKE ‘%$results%’) and I have no idea why or how to switch it off. Can anyone help? I don’t mind which way I achieve it but I must have it so that if the user puts in text into the search field it looks for that text in EITHER the name field OR the description field, rather than excludes all records unless the search text is in BOTH fields which is what is happening at present. Advice?

    Thanks,

    Nikki

    #165543

    sc0ttkclark
    Keymaster
    Post count: 813

    Try setting your findRecords $params['search'] to false. By doing this, you can roll your own search however complex it is. Be sure to search the fields you want in that case though!

    #165544

    macnikki
    Member
    Post count: 1

    Thanks for the tip. Does this mean that I would then need to develop my own search form rather than using the form together with getFilters?

    I noticed that when I first go to my page listing my entries the WHERE statement in the sql shows correctly. But, when I enter a phrase in the search box it then adds in the odd "t.name LIKE %$result% AND". Is this expected behaviour? If so, doesn’t that mean that using the getFilters function will always prevent someone from finding any records unless both the name field contains the searched text and the other fields being searched? Is there any way of preventing the getFilters function to add this extra "AND" in?

    #165545

    sc0ttkclark
    Keymaster
    Post count: 813

    You don’t have to roll your own getFilters, you can use that still if you’d like. The point is that findRecords handles the getFilters form submission on it’s own, and does the t.name thing itself. Like I said above, add the ‘search’ => false in the findRecords $params to roll your own search, just add your own t.name LIKE "%$result%" and be sure to sanitize using pods_sanitize before putting your search variable into your $params['where']

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

Wordpress Cloud Hosting