figuring out some search or filter example

0

I wanted to share some verbosely commented code for creating usable filters or searches. These are collated from a number of sources, including suggestions from Scott and other forum posts.

The lines below are for searching multiple fields of a member directory. You will likely need to change some field names to get it to work with your field names. This code belongs inside a Pods page template, and has only been tested in Pods 1.7.8.

<pre><?php // sanitize the search param from the URL if (isset($_GET["search"])) { $result = addslashes($_GET["search"]); } // invoke Pods with the chosen pod as the param $Record = new Pod('members');

// the param 'member_type' is a pick field that is part of the members Pod, you can add multiple picks as a comma-delimited list // the param 'Search' just changes what the submit button says echo $Record->getFilters('member_type','Search');

// 'name ASC' returns results in Ascending order -- alpha by name // 10 is return 10 results on a page // %$result% is the user input from the textfield (see the if statement above) $Record->findRecords('t.name ASC', 10, "(t.name LIKE '%$result%' OR (t.companyname LIKE '%$result%' OR t.city LIKE '%$result%'))");

// display pagination for the results, if needed echo $Record->getPagination();

if($Record->getTotalRows()<1){echo 'No records found. Please try your search again.';} // uncomment the line below to echo the sql statement generated by findRecords() -- invaluable for troubleshooting // echo $Record->sql;

// obviously, a template named member_list needs to exist. echo $Record->showTemplate('member_list'); ?></pre>

Having t.name LIKE '%result%' OR at the beginning of the third findRecords param is essential because of other SQL statements added by the Pods class in findRecords(). If you want to search multiple text fields, you will need to edit the final param for findRecords(). You can put as many pods fields as you like inside the parens seperated by OR. The parenthesis are essential if you have any picks set in the getFilters() call. Be forewarned, if your database is medium-to-large, adding more LIKE - OR combinations can cause your search to crawl.

HTH.

asked Apr 7 '10 at 9:36

magi182

110

add comment
enter at least 15 characters

16 Answers

0

Whoops. the findRecords should look like:

$Record->findRecords('name ASC', 10, "(t.name LIKE '%$result%' OR (t.companyname LIKE '%$result%' OR t.city LIKE '%$result%'))");

answered Nov 25 '09 at 3:11

magi182

110

add comment
enter at least 15 characters
0

@magi182 - Thanks for helping explain this! I've also made your fix in your original topic for you.

answered Nov 26 '09 at 11:57

sc0ttkclark

2936

add comment
enter at least 15 characters
0

Hello I would like to search multiple text fields too.

In my pods, it seems : <pre> <php $Record->findRecords('name ASC', 10, "(t.name LIKE '%$result%' OR (t.companyname LIKE '%$result%' OR t.city LIKE '%$result%'))"); ?> </pre>

create a sql like : <pre><?php SELECT SQL_CALC_FOUND_ROWS DISTINCT t.* FROM @wp_pod p INNER JOIN @wp\_pod\_tbl\_bibliotheque t ON t.id = p.tbl_row_id WHERE p.datatype = 3 AND (t.name LIKE '%zr%') AND (t.name LIKE '%zr%' OR (t.companyname LIKE '%zr%' OR t.city LIKE '%zr%')) ORDER BY name ASC LIMIT 0,10 ?> </pre>

As you can see, there are 2 "t.name LIKE", and one of them is an "AND", and the condition cannot be true. Result is I don't get any result with my search form.

I will try to see what is going on...

answered Mar 19 '10 at 8:30

nicomollet

1

add comment
enter at least 15 characters
0

I managed to do what I want. Just by stripping the parenthesis.

<pre> <?php //this works $Record->findRecords('t.name ASC', 20,"t.name LIKE '%$result%' OR t.other LIKE '%$result%'");

//this won't work $Record->findRecords('t.name ASC', 20,"(t.name LIKE '%$result%' OR t.other LIKE '%$result%')"); ?> </pre>

answered Mar 19 '10 at 12:55

nicomollet

1

add comment
enter at least 15 characters
0

I have added the above code to a template and have created a page with the following code

<pre> <?php $Record = new Pod('members'); $Record->findRecords('name ASC', 10); echo $Record->showTemplate('search_template'); </pre>

There's a couple of things not quite working though:

  1. When I first load the page I get the dropdown, search field and search button correctly but I also get "No records found. Please try your search again." under this as well as 3 other dropdowns , search field and search button underneath (this seems related to the amount of members I have added i.e. if I have 6 members then 6 lots of search dropdowns, fields and buttons appear below!)

I really have no idea what I could be doing incorrectly. Please help.

answered Apr 7 '10 at 8:27

daretothink

11

add comment
enter at least 15 characters
0

I've managed to stop more than one search form to appear on the page by changing "10" in findRecords to "1".

I'm still unsure though how to remove the "No records found. Please try your search again." text on entry to the page. That should only display if no entries have been found when I perform a search.

When I do perform a search and there's no entries this doesn't display and instead it seems to direct to a blank template page?! I'm very confused with Pods!

answered Apr 7 '10 at 9:36

daretothink

11

add comment
enter at least 15 characters
0

Can anybody help with this???

answered Apr 16 '10 at 2:41

daretothink

11

add comment
enter at least 15 characters
0

@daretothink - Whatever code you put into a Pod Template gets executed once for each item that findRecords retrieves. If you have 10 items, it's executed 10 times. That's why you're seeing 6 different sets of filters.

A little recap... all of the code below goes into a Pod Page.

<pre> <?php // Load the pod $Record = new Pod('members');

// Find members that meet certain criteria $Record->findRecords('name ASC', 10);

// For each returned item, display it using a template // Example of the "search_template" Pod Template code: <p>{@name}</p> echo $Record->showTemplate('search_template');

// Add search+filters if needed echo $Record->getFilters();

// Add pagination if needed (only appears if there are 2+ pages) echo $Record->getPagination(); </pre>

answered Apr 16 '10 at 2:59

logikal16

249

add comment
enter at least 15 characters
0

Thank you, thank you, thank you. You are a lifesaver. That all works perfectly. I was almost there but was just getting a little mixed up between templates and pages.

answered Apr 18 '10 at 5:45

daretothink

11

add comment
enter at least 15 characters
0

Just one more thing with this. I've done everything as you mentioned but the order of the page has the names and then the search box under the list of search results. How do I order it so the search box is at the top and then the results appear below?

answered Apr 19 '10 at 5:29

daretothink

11

add comment
enter at least 15 characters
0

Move the "getFilters" line above the "showTemplate" line.

answered Apr 19 '10 at 7:49

logikal16

249

add comment
enter at least 15 characters
0

Thanks once again logikal16 - really appreciate your help.

answered Apr 19 '10 at 1:25

daretothink

11

add comment
enter at least 15 characters
0

Very pedagogic, thanks for this explanation!

Though, I have an additional question. Is there a way to tweak the getFilters() so that it for example doesnt read 'å', 'ä', 'ö' as 'a'?

answered Jul 28 '10 at 3:35

bjornet

1

edited Jul 28 '10 at 3:52

add comment
enter at least 15 characters
0

I would like to add some code that helped me make my CONTENT SEARCH work along with the Pods methods (such as getFilters, findRecords, getPagination). I wanted to be able to search by several words, so that if I searched on "Mar Bos" That could apply to all words in several diffrent Pod-columns (name, city, company etc). Here it could be Montreal, Maria, Boston, Bruce etc.

What I did was: 1. Separate the search into an Array. 2. Create a variable -> that holds a string -> containing an WHERE-segment in a SQL-question. 3. That variable, $where, where put into the $sql-slot in the findRecords()-method.

That made it valid code for the following methods s.a. getPagination().

Examplecode:

    <pre><?php 
    // sanitize the search param from the URL 
    if (isset($_GET['search'])) { 
        $search_param = explode(' ', addslashes($_GET['search']));
    }else{
        $search_param = array('');
    }
    print_r($search_param);

    // PODs insertion
    // invoke Pods with the chosen pod as the param 
    $testRecord = new Pod('member');

    // the param 'member_type' is a pick field that is part of the members Pod, you can add multiple picks as a comma-delimited list 
    // the param 'Search' just changes what the submit button says echo 
    $testRecord->getFilters('','Search');

    $where = ''; // Starting the where builder
// For each search word, make a new OR in the SQL WHERE
    foreach ($search_param as $search_word) {
        $where .= "(t.name LIKE '%$search_word%' 
        OR t.company LIKE '%$search_word%'
        OR t.city LIKE '%$search_word%') OR ";
    }
    // Remove the last " OR "
    $where = substr($where, 0, strlen($where) - 4);

    $testRecord->findRecords('name ASC', 10, $where);

    while ( $testRecord->fetchRecord()){
        // set our variables
        $testRecord_name       =   $testRecord->get_field('name');
        $testRecord_company     =  $testRecord->get_field('company');
        $testRecord_city       =   $testRecord->get_field('city');

        ?>

        <div id="test">
            <h2>
            <?php 
                echo $testRecord_name;
            ?>
            </h2>
            <p><?php echo $testRecord_company; ?></p>
            <p><?php echo $testRecord_city; ?></p>
        </div><br>

        <?php                
    }                   

    // display pagination for the results, if needed 
    echo $testRecord->getPagination();

    if($testRecord->getTotalRows()<1){
        echo 'No records found. Please try your search again.';
    } 
    ?></pre>

Hope someone get helped by it.

answered Aug 3 '10 at 10:22

bjornet

1

edited Aug 3 '10 at 10:23

add comment
enter at least 15 characters
0

Can you post your full code in regards to $where?

Also, the t. must be prefixed for all fields on the current pod. It's known as an alias in MySQL, and references the "t" table, which is aliased on the current pod's data table. You prefix pick column names for relating to another pick column's pod data.

answered Aug 10 '10 at 3:40

sc0ttkclark

2936

add comment
enter at least 15 characters
0

First time using PODS so apologies if this is an obvious error.

i have got two dropdowns and a search field working but when I go onto the page all the records are already displayed under the search form. When I perform a search then they disappear.

Any help would be great thanks, using same code as above

Thank you

answered Aug 10 '10 at 2:44

inspiration

7

edited Aug 10 '10 at 4:59

add comment
enter at least 15 characters