Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Detecting no search results and pager component
Two questions:
How does one detect if there are zero search results for a search term? I would like to notify the user there were no results returned. Textpattern returns nothing as a result.
How does one page search results? I don’t want to display all results in one page. I would like a means to limit the number of articles returned as search results (say 100) and a means to have a pager navigation component to show, say, 10 results per page. It should also have navigational links to move among pages of search results.
Is there a plug in to pull this off?
Kerry Kobashi
Kobashi Computing
Offline
Re: Detecting no search results and pager component
I use this in my page template which solves both of your problems:-
<txp:article pgonly="y" />
<h3 id="searchResultTitle"><txp:search_result_count text="search results for" /> "<txp:page_url type="q" />"</h3>
<txp:article form="search_results" limit="10" sort="posted desc" />
For your first problem the <txp:article pgonly="y" />
tag is the important bit as it makes TXP scan the results before outputting the “h3”.
For your second problem the “limit” attribute in the second article tag makes the results page in batches of 10. You will need to have the “older” and “newer” paging tags in place of course.
Make this page your best buddy. :)
Last edited by thebombsite (2008-07-19 17:25:44)
Stuart
In a Time of Universal Deceit
Telling the Truth is Revolutionary.
Offline
Re: Detecting no search results and pager component
If you want to display another message rather than “0 search results for …”, you can use txp:if_search_results together with <txp:else />
.
There is also txp:search_term which I think is now the official tag for page_url type="q"
(which also still works).
TXP Builders – finely-crafted code, design and txp
Offline
Re: Detecting no search results and pager component
Thanks guys for the response. Let me explain what I’m doing. I have a separate page template called ‘search’ that uses the searchform parameter. I chose this because intuitively it says this form gets called in search results:
Page template: search
(snip...)
<div class="search-results">
<txp:article searchform="search-result"/>
(snip...)
</div>
(snip...)
Here is my form that gets iterated over for each search result:
Form: search-result
<div class="search-result">
<h3><txp:permlink><txp:search_result_title /></txp:permlink></h3>
<p>
<span class="text"><txp:search_result_excerpt hilight="strong" limit="3"/></span><br/>
<span class="url"><txp:search_result_url/></span>
</p>
</div>
This all works fine as I get search results. However every result is shown on one page (imagine 1000 hits) and it does not handle the case if there are zero results. In other words, its impractical.
After reading jakob’s suggestion to solve the zero result case, I tried to implement this in the form and it didn’t work:
<div class="search-result">
<txp:if_search_results>
<h3><txp:permlink><txp:search_result_title /></txp:permlink></h3>
<p><span class="text"><txp:search_result_excerpt hilight="strong" limit="3"/></span><br/><span class="url"><txp:search_result_url/></span></p>
<txp:else/>
<p>No results found for <strong><txp:search_term></strong>.</p>
</txp:if_search_results>
</div>
When there were no search results, the else statement never got invoked. Is this a bug? It appears to me that after the call to txp_article searchform=“search” the if_search_results tag is completely ignored.
It also didn’t work at the page level. To me it shouldn’t work because the call is too late to get into the iteration hook:
<div class="search-results">
<h2>Search Results</h2>
<form method="get" action="/search/">
<input type="text" name="q" value="" size="30" /> <input type="submit" value="Search" />
</form>
<txp:article searchform="search-result"/>
its too late because we can't get into the hook - we've already iterated
<txp:if_search_results max="100">
<p>
this doesn't make sense to use it here as we already have iterated over the search result and max/min parameters are therefore meaningless
</p>
<txp:else/>
<p>No search results</p>
</txp:if_search_results>
</div>
I’ve Googled if_search_results and came up with nothing of assistance. In the textbook examples, there is no context on how to use txp:if_search_results relative with the other txp search tags. I ask humbly, is this tag even implemented and known to work in the context it was meant for? It appears the tag was meant for a very specific event BEFORE output is to be rendered, not after. But I have no indication just how to get into that pre-hook. Perhaps thebombsite’s solution gives some hints but it is not intuitively obvious.
Unless I am doing something terribly wrong, <txp:article searchform=“search-result”/> seems useless. All it appears to do is spit out the entire search result in one big page only allowing one to fiddle with the presentational aspects of the text. But it allows no control over the paging or detection of zero results which is such a common thing to do in practical sense.
Last edited by kkobashi (2008-07-19 22:25:09)
Kerry Kobashi
Kobashi Computing
Offline
#5 2008-07-19 22:18:12
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: Detecting no search results and pager component
Some tips for displaying code ;)
- use
bc.
for code without blank lines in it - use
bc..
for code with blank lines; escape by starting the next paragraph withp.
- always have a blank line before
bc.
orbc..
and after your code block
Offline
Re: Detecting no search results and pager component
Thanks Els.. I been trying to reformat :O)
Kerry Kobashi
Kobashi Computing
Offline
#7 2008-07-19 22:25:06
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: Detecting no search results and pager component
<txp:if_search_results>
should go on the page, not in the form. It also should be preceded by an article tag, otherwise it doesn’t have anything to count. If you want to display your search results later on the page, you need to use <txp:article pgonly="1" />
, it won’t display any articles but it makes a count for if_search_results, search_result_count or pagination possible.
I don’t think setting a maximum of search results is possible when you also want to use pagination. The limit
attribute sets the number of articles that is displayed on one page.
Offline
Re: Detecting no search results and pager component
Thanks Els…
<txp:article pgonly="1"/>
<txp:if_search_results>
<txp:article searchform="search-result" limit="5" />
<txp:else/>
<p>No articles found.</p>
</txp:if_search_results>
I tried the above in the search page and it worked.
So from my understanding, the pgonly=“1” is like making a mysql count call but without actually holding onto the resultset. I can imagine that by doing so, whatever global variables that textpattern is holding actually gets set at that point. Whereas without the count like how I was using it above, they were unset and causing me problems. Thanks for help.
By the way, pgonly isn’t very intuitive… it should be named countonly
Last edited by kkobashi (2008-07-19 22:50:51)
Kerry Kobashi
Kobashi Computing
Offline
Re: Detecting no search results and pager component
<txp:article pgonly="1"/>
<txp:if_search_results>
<txp:article searchform="search-result" limit="3" pageby="3" />
<div id="oldnewlinks">
<txp:older>older</txp:older> <txp:newer>newer</txp:newer>
</div>
<txp:else/>
<p>No articles found.</p>
</txp:if_search_results>
Ok now I’m having problems showing the older and newer links. Nothing comes up. Any ideas?
Last edited by kkobashi (2008-07-19 23:31:03)
Kerry Kobashi
Kobashi Computing
Offline
Re: Detecting no search results and pager component
Not sure if this will sort it but you don’t need “pageby” as this is automatically set to the same as “limit”, also remember the gap before the forward slash, so <txp:article />
, <txp:else />
etc. Other than that I can’t see anything wrong with your code at the moment.
As for “pgonly” being renamed “countonly” – it isn’t just for counting. It lets TXP parse through the page without outputting anything, so it can pick up one or two other things apart from an article count. I will admit it is a slightly confusing term but it’s the best option at the moment. ;)
Stuart
In a Time of Universal Deceit
Telling the Truth is Revolutionary.
Offline
Re: Detecting no search results and pager component
thebombsite wrote:
also remember the gap before the forward slash
With the current parser there can be gap or no gap, it’s the same. <txp:else/>
works just fine, and so does <txp:else />
.
Btw, also TXP’s error messages show tags without the space ;)
Last edited by Gocom (2008-07-20 03:34:36)
Offline
Re: Detecting no search results and pager component
hey guys thanks for the responses
I figured the gap is no big deal because my code is running fine with it in all my pages.
I took the pageby out but it didn’t make any difference – still the same result. I’m still having the same problem at this moment… no output for the older and newer tags. I look at the output view source and the container div is there but the older/newer tags don’t get printed.
I’m staring right at the source code at the moment in publish.php (version 4.06) on lines 667 to 700 in doArticles and trying to determine what is going on.
Last edited by kkobashi (2008-07-20 06:39:15)
Kerry Kobashi
Kobashi Computing
Offline