Search Problem - Displaying all records under search boxes
I have made a wordpress template file with the following code it it.
<?php
// sanitize the search param from the URL
if (isset($_GET['search'])) {
$search_param = explode(' ', addslashes($_GET['search']));
}else{
$search_param = array('');
}
// PODs insertion
// invoke Pods with the chosen pod as the param
$testRecord = new Pod('offers');
// 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('business_type,county','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.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_companylogo = $testRecord->get_field('company_logo');
$testRecord_city = $testRecord->get_field('city');
?>
<div id="test">
<h2>
<?php
echo $testRecord_name;
?>
</h2>
<img src="<?php echo $testRecord_companylogo; ?>" />
<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.';
}
?>
It displays two dropdowns and a search box but with all records already listed underneath when I go to the page.
Is there a way to fix this?
Also can the results page be a different WP template?
Any help would be really appreciated.
Thanks
edited Aug 10 '10 at 4:54
11 Answers
It looks like there may be some issues with the findRecords. First, take out the " OR " at the end. Second, put $Record->search_var = 'search2'; before your findRecords. Third, you'll want to include your other PICK fields in your findRecords. Since you're using a custom search, you'll want to reference them by id, like "business_type.id=$business_type" and "county.id=$county" if a value was set. Then it should be working the way you're expecting.
Thank you for the quick reply.
If I remove the "OR" at the end I get an error and it stops working altogether.
Also I'm not really sure what you mean with the other two steps.. first time using Pods and I'm no php expert.
Dev site can be seen here - http://www.inspirationdev.com/gosave/alan-test/ I am really struggling with the Searching of pods so any more help at all would be great.
Thanks
edited Aug 10 '10 at 7:21
Scott means that with the current template your $where will aways end with an OR, while you need it only between the statements with LIKE.
$where = ''; // Starting the where builder
// For each search word, make a new OR in the SQL WHERE
foreach ($search_param as $search_word) {
if (!empty($where)) $where .= " OR ";
$where .= "(t.name LIKE '%$search_word%'
OR t.city LIKE '%$search_word%')";
}
This is something like sculpting with hammers, but should work for a start
Thanks Fike. It is working now without the OR but only if I also remove the line:
$where = substr($where, 0, strlen($where) - 4);
..but it is still displaying all the results on the page before a Search is made.
Surely there a more simple/standard way of performing a search?? I have looked through all the posts and they're all similar to this.
This is a big problem with Pods.
edited Aug 11 '10 at 12:05
> but with all records already listed
That's because your pods search is performed even on a blank request, your code launches findRecords regardless of getting request or not - it just passes it to findRecords in case it exists, but findRecords just lists everything, if you haven't specified where clause the case it doesn't exist). new Pod, findRecords, fetchRecords and all other stuff should be done only server has received a valid request. As i've understood by
else{
$search_param = array('');
}
you want an option to list everything for blank request. Then you need to add something like
<input type="hidden" name="performsearch" value="true">
to your form, and wrap all Pods output in
if ( $_GET['performsearch'] === "true" ) { here's the output }
Or (even better, i think) you can just use your already existing "type" field:
if ( isset($_GET['type']) ) { here's the output }
edited Aug 11 '10 at 12:46
Thank you for the help Fike but I am really lost now.
I am trying to piece this Search function together from other users posts, I don't really have PHP skills
Would you be willing to take a look at this for me directly and I would pay you for your time?
edited Aug 11 '10 at 1:00
I have it almost working.. Is there a way to re-direct to another page to display the results?
i'm no php expert too, so hope this will work and what you need (at least N++ says it has correct amount of brackets and stuff)
<?php
// we are checking here if user has already submitted a search or just has loaded a page
if (isset($_GET['type'])) {
// sanitize the search param from the URL - just left that
if (isset($_GET['search'])) {
$search_param = explode(' ', addslashes($_GET['search']));
}else{
$search_param = array('');
}
// PODs insertion
// invoke Pods with the chosen pod as the param
$testRecord = new Pod('offers');
// 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('business_type,county','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.city LIKE '%$search_word%') OR ";
}
$where = substr($where, 0, strlen($where) - 4);
$testRecord->findRecords('name ASC', 10, $where);
// to be honest i don't remember where exactly getTotalRows should go, but most probably and logical is that it should be here
if ( $testRecord->getTotalRows()<1 ) echo '<h2> Sorry, there wasn\'t any match for your search criteria.</h2>';
else {
while ( $testRecord->fetchRecord() ){
// set our variables
$testRecord_name = $testRecord->get_field('name');
$testRecord_companylogo = $testRecord->get_field('company_logo');
$testRecord_city = $testRecord->get_field('city');
?>
<div id="test">
<h2><?php echo $testRecord_name; ?></h2>
<img src="<?php echo $testRecord_companylogo; ?>" />
<p><?php echo $testRecord_city; ?></p>
</div>
<?php
}
// display pagination for the results, if needed
echo $testRecord->getPagination();
}
}
else echo 'This is a search!';
?>
> I have it almost working.. Is there a > way to re-direct to another page to > display the results?
you can use "action" attribute in your form tag like this
<form action="/scripts/search.php">
or like this
<form action="<?php bloginfo('template_url'); ?>/search.php">
also, you can use redirect from any php file by header() func before any output
edited Aug 11 '10 at 2:06
Thank you so much for your time Fike. I have tried using the above and now the page displays only the echo "This is a search!"
Maybe I am doing this all the wrong way from the start - I am putting all this code above into a Wordpress page template. (along with the rest of the page layout)
Should I be putting it into it's own file something like podsearch.php? And then calling that as a form action like you wrote above?
edited Aug 11 '10 at 3:10
I assumed this is just a part of the code that goes in the same template as forms. If you did the same thing, results should appear only after you've submitted something, and before that it should just say 'This is a search'.
About templates: actually, there is no difference in processing with that, but if you want to add some more pages with plain text, images, contact forms, whatever to your site (99% that you do), then you need to have two separate templates for that - one just for displaying anything you type in admin panel, and the second one for showing pods data. So you need to create a new file (or just copy your current theme page.php), name it as you wish (in lowercase and without whitespaces, podsearch.php is ok), and then replace WP loop (construction that begins with 'if have_posts()') with your forms and pods code. The last thing that should be done is putting
<?php /* Template Name: Pod Search */ ?>
right in the beggining of the file. That allows WP to see it as valid page template and assign this template to any page.
My explanations are a bit messy, i hope that you'll understand them. The goal is to bring up a .php template that will contain typical page look (header, footer, etc.), search submit forms, and code for displaying pods data; then create a page for it.
That's some basics and it's hard to get inside php/WP logic in a minute, i guess (for example, i still don't understand some comment functions and just have made my own template for commenting). You can send me your page.php from WP theme to fike-at-ambinight.com (i'll also need pod name and column machine names whose content is searched for requested words), i'll try to bring everything together, but if you want to get into it, you'll probably want to build it by yourself.
edited Aug 11 '10 at 5:10
Just an FYI, Pods automatically searches data based on the Name and no other fields (besides any PICK fields enabled in getFilters). What you're effectively trying to do is customize that, which is not a problem and Pods can handle it easily, it's just that it requires a bit of work via PHP and I'm sorry you've been having trouble with it. I'd like to help further, so I'll check back on this thread later tonight when I have some more time and see if I can solve your issue if you haven't already figured it out.


