1 of 3
1
Plugin: Search Fields
Posted: 29 September 2009 04:39 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  251
Joined  10-08-2002

Use this plugin to search weblog entry titles, custom fields, category names, category descriptions and category custom fields.

Search parameter syntax is identical to the weblog search parameter.

In addition to entry custom fields you can search entry titles, category names, category descriptions and category custom fields. When searching categories references to category fields should be prefixed with ‘cat_’.

For example:
search:cat_name=“keyword”
search:cat_description=“keyword”
search:cat_custom_field=“keyword”

Returns a delimited list of entry ids.


Parameters
————————

search:[field] = (optional) Field can be title, cat_name, cat_description, [custom_field_name], cat_[custom_field_name].

weblog = (optional) Single weblog name to search. Default is * (searches all weblogs).

operator = (optional) ‘AND’ or ‘OR’. Operator for joining search field WHERE conditions. Default is ‘OR’.

delimiter = (optional) Delimiter for returned entry id string. Default is pipe |.

placeholder = (optional) Single variable placeholder to replace with search results output. Default is search_results (use as {search_results}).

dynamic_parameters = (optional) Eg: “title|custom_field”. Allow specific search parameters to set via $_POST (form fields should have same name as the fields you wish to search).


This plugin is best used as a tag pair wrapping {exp:weblog:entries}.

Example
——————

{exp:search_fields search:title="keyword" search:custom_field="keyword" search:cat_name="keyword" operator="OR" weblog="my_weblog" parse="inward"}
    {exp
:weblog:entries entry_id="{search_results}" dynamic="off"}
        
<a href="{page_url}">{title}</a>
    
{/exp:weblog:entries}
{
/exp:search_fields} 
File Attachments
pi.search_fields.zip  (File Size: 4KB - Downloads: 451)
Profile
 
 
Posted: 29 September 2009 08:02 AM   [ Ignore ]   [ # 1 ]  
Professor
Avatar
RankRankRankRankRankRankRank
Total Posts:  13726
Joined  04-15-2006

Hi Mark,

A really nice neat little addition to all the plugins out there this one so thanks for that grin

Any ideas how much overhead this applies to a page in getting the entries like this? I mean, having a plugin supplying the entry ids to the weblog tag like this?

Anyhow a really nice little plugin which I’m sure will come in handy when needing to search titles so thanks for the upload.

Best wishes,

Mark

 Signature 

Shopping Cart Plugin | Full List Of Add-Ons | About Me
——————————————————————————————
2.x Bug Tracker | Upgrade Errors

Profile
 
 
Posted: 29 September 2009 08:21 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  251
Joined  10-08-2002

There is a very small additional overhead for searching entry titles (compared to using exp:weblog:entries search:field=’‘) as I’m using an extra join. Searching category titles and description adds a further two joins, and searching custom category fields another join and a second query. These additional joins are only made as needed, so if you’re just searching titles the overhead is only one join. Narrowing the search using the weblog parameter will help with speed, and using the exp:weblog:entries disable parameter will allow you to reduce the total number of queries, so be sure to do that for features not required.

So this isn’t quite a replacement for the search module, it doesn’t cache results and will probably struggle if you have a very large number of entries; however, it should be fine for custom search forms on sites with a few thousand entries per weblog.

Profile
 
 
Posted: 29 September 2009 08:25 AM   [ Ignore ]   [ # 3 ]  
Professor
Avatar
RankRankRankRankRankRankRank
Total Posts:  13726
Joined  04-15-2006
Mark Croxton - 29 September 2009 12:21 PM

There is a very small additional overhead for searching entry titles (compared to using exp:weblog:entries search:field=’‘) as I’m using an extra join. Searching category titles and description adds a further two joins, and searching custom category fields another join and a second query. Narrowing the search using the weblog parameter will help with speed, and using the exp:weblog:entries disable parameter will allow you to reduce the total number of queries, so be sure to do that for features not required.

Yep I always add those in as standard anyway wink Definitely the best thing to do on any site.

Mark Croxton - 29 September 2009 12:21 PM

So this isn’t quite a replacement for the search module, it doesn’t cache results and will probably struggle if you have a very large number of entries; however, it should be fine for custom search forms on sites with a few thousand entries per weblog.

Sounds good to me grin

Thanks again for a great plugin addition. I’m sure many others are going to love this one.

Best wishes,

Mark

 Signature 

Shopping Cart Plugin | Full List Of Add-Ons | About Me
——————————————————————————————
2.x Bug Tracker | Upgrade Errors

Profile
 
 
Posted: 15 October 2009 06:56 PM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  100
Joined  01-24-2006

This plugin is great, however, any suggestions on how to pass values dynamically to it would be welcome!

For example, I am trying to do this: 

{exp:search_fields search:title="<?php echo $_GET['q'] ?>"  search:cf_pros_firstname="<?php echo $_GET['q'] ?>" search:cf_pros_lastname="<?php echo $_GET['q'] ?>" operator="OR" weblog="<?php echo $_GET['q'] ?>" parse="inward"}
    {exp
:weblog:entries entry_id="{search_results}" dynamic="off"}
        {title}
|{url_title} 
    {
/exp:weblog:entries}
{
/exp:search_fields} 

... I am trying to use it in conjunction with an ajax auto-complete plugin found here.

I hope that’s all clear grin

 Signature 

Vibe9 Design - Vancouver Web Design Company

Profile
 
 
Posted: 15 October 2009 07:08 PM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  100
Joined  01-24-2006

Duoh!  Found the solution: you have to set the template PHP parse setting to “input”.

 Signature 

Vibe9 Design - Vancouver Web Design Company

Profile
 
 
Posted: 16 October 2009 04:14 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  251
Joined  10-08-2002

Glad you worked it out. One very important thing I’d suggest though if you’re doing it this way is sanitize the $_GET values before passing them to the script. EE provides the Input class for this. I would also add in some checking to prevent XSS (if you plan on re-displaying the search term):

<?php
global $IN
$q str_replace(array('{','}','?','\"','\\'), ''trim($q $IN->GBL('q''GET')));
?> 

Alternatively you can use the dynamic_parameters parameter to tell the script to grab the submitted values from the $_POST array, although you would need to duplicate the field values if you’re searching in more than one custom field for the same string.

Profile
 
 
Posted: 16 October 2009 11:56 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Rank
Total Posts:  100
Joined  01-24-2006

Cool thanks for the tip.  Should I include that code once in the search result template or for every instance where I’m currently using the following?

<?php echo $_GET['q'] ?> 
 Signature 

Vibe9 Design - Vancouver Web Design Company

Profile
 
 
Posted: 22 October 2009 04:29 AM   [ Ignore ]   [ # 8 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  251
Joined  10-08-2002

I would suggest for every instance that you register a global variable smile

Profile
 
 
Posted: 07 January 2010 10:19 AM   [ Ignore ]   [ # 9 ]  
Lab Assistant
RankRank
Total Posts:  173
Joined  02-23-2009

This is just what I’m looking for. But: when I try to install it, it kills my plugin manager (page is completely blank). According to the knowledge base - http://expressionengine.com/knowledge_base/article/my_admin_plugin_manager_page_is_blank/ - that’s normally due to a PHP parse error or permissions. I’ve set permissions to 777 and still not working, so might there be an error in the code?

Profile
 
 
Posted: 07 January 2010 10:26 AM   [ Ignore ]   [ # 10 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  251
Joined  10-08-2002

I’ve only tested the plugin on EE 1.6.8. It definitely won’t work for 2.0, and maybe not for earlier versions.

What is your EE version?

Profile
 
 
Posted: 07 January 2010 10:27 AM   [ Ignore ]   [ # 11 ]  
Lab Assistant
RankRank
Total Posts:  173
Joined  02-23-2009

I’m on 1.6.8 too… was a new install but tried some other plugins and they worked ok…

Profile
 
 
Posted: 07 January 2010 10:33 AM   [ Ignore ]   [ # 12 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  251
Joined  10-08-2002

Scratching my head here… try turning on PHP error messages here:

Admin > System preferences > Output and debugging preferences

After that, take a look at that plugin page and tell me what you see.

Profile
 
 
Posted: 07 January 2010 10:40 AM   [ Ignore ]   [ # 13 ]  
Lab Assistant
RankRank
Total Posts:  173
Joined  02-23-2009

Debug Preference was already set to show messages to Super Admins. Maybe my hosts are blocking the PHP errors? Will check it out…

Profile
 
 
Posted: 07 January 2010 04:11 PM   [ Ignore ]   [ # 14 ]  
Lab Assistant
RankRank
Total Posts:  173
Joined  02-23-2009

Have enabled PHP errors via a .htaccess. Here’s what I got:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, .(JavaScript must be enabled to view this email address) and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

... Is that any use?

Profile
 
 
Posted: 07 January 2010 04:36 PM   [ Ignore ]   [ # 15 ]  
Lab Assistant
RankRank
Total Posts:  173
Joined  02-23-2009

Hmm, that seemed to be a problem with my .htaccess file: by adding php_flag display_errors on, to .htaccess in the root of the site, I get that error for every page. Have tried a couple of other ways to get htaccess to show PHP errors and they all have the same effect.

Any ideas? Thanks for your help and sorry if I’m missing something obvious - been staring at my screen for a long time now!

Profile
 
 
Posted: 08 January 2010 05:26 AM   [ Ignore ]   [ # 16 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  251
Joined  10-08-2002

Yeah that’s just an error with your htaccess - never mind.

Could you let me know what version of PHP and MySQL you are running?

Also, have you considered that your zip decompressor might have corrupted the plugin file? Maybe you could check the plugin file with something like BBEdit’s Zap Gremlins, or just scan the code for odd looking characters…

Thanks

Mark

Profile
 
 
Posted: 08 January 2010 05:30 AM   [ Ignore ]   [ # 17 ]  
Lab Assistant
RankRank
Total Posts:  173
Joined  02-23-2009

Yeah it was - I was using pretty standard stuff I thought, but 1&1 didn’t like it. I also just remembered that 1&1 defaults to PHP4 unless you tell it to use PHP5. Have fixed that, and your plugin now appears fine in the list! (Although now the rest of my site’s stopped working, but I think that’s something to do with PHP, not your plugin.)

Thanks for your help - sorry to miss such an obvious one!

Profile
 
 
Posted: 12 January 2010 01:07 PM   [ Ignore ]   [ # 18 ]  
Summer Student
Avatar
Total Posts:  23
Joined  03-24-2009

Mark, here is a worthwhile addition to allow for querying against multiple weblogs:

// limit to a weblog?
        
if ($weblog !== '*')
        
{
            
// Added by Brian Litzinger
            
if(substr($delimiter$weblog))
            
{
                $weblogs 
explode($delimiter$weblog);
                
$sql_conditions .= "AND (";
                foreach(
$weblogs as $wb)
                
{
                    $sql_conditions 
.= "wl.blog_name = '{$DB->escape_str($wb)}' OR ";
                
}
                $sql_conditions 
substr($sql_conditions0, -4);
                
$sql_conditions .= ")";
            

            
else 
            
{
                $sql_conditions 
.= "AND wl.blog_name = '{$DB->escape_str($weblog)}'";
            
}
        } 
 Signature 

Twitter: @the_nerdery
URL: http://nerdery.com

Profile
 
 
   
1 of 3
1