Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2015-10-03 14:56:11

vlad
New Member
Registered: 2015-10-03
Posts: 7

Will not display "no results found" when I search

Hello guys,

I have a problem on my search page where if I enter a query that doesn’t exist, instead of displaying “no results found”, it shows nothing. Just the header and footer.

Here is the entire code to my search_results form.

<div class="search">
<txp:if_first_article>
<txp:search_input />
<p class="search-found"><txp:search_result_count /> matching "<txp:search_term />"</p>
</txp:if_first_article>
<txp:if_search_results>
<txp:else />
<txp:search_input />
<h3>NOTHING FOUND</h3>
</txp:if_search_results>
<div class="search-listing">
<a href="<txp:permlink />" itemprop="url"><h4 class="search-title"><txp:title /></h4></a>
<p class="search-excerpt"><txp:search_result_excerpt /></p>
<a href="<txp:permlink />"><txp:permlink /></a>
</div>
</div>

I realize that I trimmed a lot of the code but even when I use the default it still doesn’t work.

Also worth noting that prior to editing this code, everything would appear as many times as there were search results found. So if I had 5 results found, I would see 5 set of pagination links and 5 of everything basically.

Thanks in advance.

Last edited by vlad (2015-10-03 15:14:42)

Offline

#2 2015-10-03 20:29:53

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Will not display "no results found" when I search

vlad wrote #295363:

Also worth noting that prior to editing this code, everything would appear as many times as there were search results found. So if I had 5 results found, I would see 5 set of pagination links and 5 of everything basically.

Hi Vlad, welcome to the forum!

Just remember that this form will be called for every found article, whence:

  • the form is not even used if nothing is found.
  • the content of the form is output 5 times if 5 articles are found.

Offline

#3 2015-10-04 03:11:55

vlad
New Member
Registered: 2015-10-03
Posts: 7

Re: Will not display "no results found" when I search

Thanks for the response etc. What you’re saying did cross my mind but still hoping there’s a fix.

Offline

#4 2015-10-04 04:56:26

jstubbs
Moderator
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: Will not display "no results found" when I search

Hi Vlad,

Not sure if you remembered to add the <txp:article /> tag..try the following code:

<txp:if_search>	
<div class="search">
	<txp:article pgonly="1" limit="100" />
	<txp:if_first_article>
		<txp:search_input />
		<p class="search-found"><txp:search_result_count /> matching "<txp:search_term />"</p>
	</txp:if_first_article>
	<txp:if_search_results>
		<h3>STUFF FOUND</h3>
	<txp:else />
		<txp:search_input />
		<h3>NOTHING FOUND</h3>
	</txp:if_search_results>
	<div class="search-listing">
		<a href="<txp:permlink />" itemprop="url"><h4 class="search-title"><txp:title /></h4></a>
		<p class="search-excerpt"><txp:search_result_excerpt /></p>
		<a href="<txp:permlink />"><txp:permlink /></a>
	</div>
</div>
<txp:else />
NON-SEARCH RELATED STUFF...
</txp:if_search>

Offline

#5 2015-10-04 11:37:28

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Will not display "no results found" when I search

vlad wrote #295370:

Thanks for the response etc. What you’re saying did cross my mind but still hoping there’s a fix.

Sure, just put things where they belong:

Page

...
<txp:article pgonly="1" />
<txp:search_input />
<txp:if_search>
	<txp:if_search_results>
		<p class="search-found"><txp:search_result_count /> matching "<txp:search_term />"</p>
		<txp:article />
	<txp:else />
		<h3>NOTHING FOUND</h3>
	</txp:if_search_results>
<txp:else />
	...
</txp:if_search>
...

Form search_results

<div class="search">
	<div class="search-listing">
		<a href="<txp:permlink />" itemprop="url"><h4 class="search-title"><txp:title /></h4></a>
		<p class="search-excerpt"><txp:search_result_excerpt /></p>
		<a href="<txp:permlink />"><txp:permlink /></a>
	</div>
</div>

PS somewhere in 4.5 the above scheme switched to

<txp:if_search>
	</txp:output_form form="search_results" />
<txp:else />
	...
</txp:if_search>

Some of the page content has then migrated to search_results form, creating some confusion.

Last edited by etc (2015-10-04 12:36:00)

Offline

#6 2015-10-08 19:48:03

vlad
New Member
Registered: 2015-10-03
Posts: 7

Re: Will not display "no results found" when I search

Thanks for the responses, I got it to work!

Here is what my default page looks like…

<txp:php>include "http://www.example.com/custom/header.php"; </txp:php>
<txp:php>include "http://www.example.com/custom/main.php"; </txp:php>
<txp:php>include "http://www.example.com/custom/footer.php"; </txp:php>

What made it work was putting the ‘Nothing Found’ code in the main.php. I had tried to put it in the header.php file prior but the code would show up on every single page I went to. Putting it in main.php makes it only show up in the search results when applicable.

Offline

#7 2015-10-10 18:14:20

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,595
Website

Re: Will not display "no results found" when I search

vlad, it looks like you’re using a txp / php mix. Unless there’s a specific reason why you’re doing it that way, the usual way of doing the setup you describe in txp is to use a structure like this (much simplified) as your page template:

<html>
  <head>
…
  </head>
  <body>
    <!-- header -->
    <txp:output_form form="header" />

    <!-- main content -->
    <txp:output_form form="main" />

    <!-- footer -->
    <txp:output_form form="footer" />

  </body>
</html>

And then to place the code for the header, main content and footer in three corresponding txp forms rather than php files.

A common method if you’re using the same page header and footer in different sections where only the “main” section changes per section is to use:

    <!-- main content -->
    <txp:output_form form='main_<txp:section />' />

in the middle (note the use of single quotes when using a tag inside the attribute of another tag). Then to create a different form for each section, e.g. main_about, main_portfolio, main_contact. You can, of course, use three separate page templates if you prefer.


TXP Builders – finely-crafted code, design and txp

Offline

#8 2015-10-28 04:57:18

vlad
New Member
Registered: 2015-10-03
Posts: 7

Re: Will not display "no results found" when I search

Thanks Jakob. May I ask why you recommend me using the forms instead? Is it simply because it will speed up the site ever so slightly? The only reason that I did it my way with the php files is because I like to edit my files in Sublime Text (sorry not in textpattern) and when I save it automatically uploads it to my server. I don’t have to mess around in my browser.

Also, I saw on your website some time ago that you mentioned NOT to do it my way unless absolutely necessary. Just curious why. Thanks.

Offline

#9 2015-10-28 09:37:48

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,595
Website

Re: Will not display "no results found" when I search

May I ask why you recommend me using the forms instead?

I’m not an expert here, but I think the main reason is that textpattern passes the various system variables to the forms and back, and if you use php blocks, you may need to initialise those variables to use them in each php block. Someone else is better qualified to answer that, but read on …

because I like to edit my files in Sublime Text (sorry not in textpattern) and when I save it automatically uploads it to my server

I like using a code editor too. There are a few plugins for that. cnk_versioning is one of them, and rah_flat is another (more reliable in my opinion) plugin that allows you to work with flat files for your pages, forms (and styles if you want). Make a copy of your database first, especially if using rah_flat as it cleans out unused forms and pages from your database. rah_flat needs installing in the plugin temporary directory (see admin > preferences), or using composer as per the instructions on github. If you have sublime text upload your files on saving to the templates directory (you can set where you want to put that yourself), then you’ll have the same workflow as you mention. There’s an example of the file structure on github.

Also, I saw on your website some time ago that you mentioned NOT to do it my way unless absolutely necessary. Just curious why. Thanks.

I don’t write an own textpattern site, so I’m not sure where I would have written that.

And there could be good reasons for using an include, for example, when sharing a section of php across textpattern and other scripts on the same homepage. Example: on one site I had a separate gallery script for a photographer with its own admin area, but I wanted to share the page navigation across both scripts so that the site looks the same. That can also be done with textpattern using rah_external_output. In that case I put the header and navigation in a textpattern form and then included that form using the link provided by rah_external_output in the template of the other gallery script…

There are many ways of doing things…


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB