Displaying "No results found" on Search page


#1

Hello,
I am trying to get a "No results found"message on my site. I’ve seen a similar topic with a solution but since my PHP knowledge is zero to none I cannot get it to work.

This is my Search template:

 <form>
 <input type="search" name="q" value="<?php echo esc($query) ?>" id="search" placeholder="Enter your keyword and press enter">
 <input type="submit" value="Search" id="submit">
</form>

<ol class="searchresults">
  <h3>Search results:</h3>
      <?php foreach($results as $result): ?>
      <li>
    <a href="<?php echo $result->url() ?>">
      <?php echo $result->title()->html() ?>
    </a>
  </li>
      <?php endforeach ?>
</ol>

This is the controller ( I’ve copied everything from the documentation):

<?php

return function($site, $pages, $page) {

  $query   = get('q');
  $results = $site->search($query, 'title|text|tags|description');
  $results = $results->paginate(20);

  return array(
    'query'      => $query,
    'results'    => $results,
    'pagination' => $results->pagination()
  );

};

What do I have to add ( and where ) to get a "no results found"message?

Many thanks for your help!


Show "No Results Found" Only After Hitting Enter
#2

Just put an if-statement around your code:

<?php
if($results) : ?>
  <h3>Search results:</h3>
  <ol class="searchresults">
   <?php foreach($results as $result): ?>
      <li>
        <a href="<?php echo $result->url() ?>">
         <?php echo $result->title()->html() ?>
        </a>
      </li>
    <?php endforeach ?>
  </ol>
<?php else : ?>
  <p>There are no results</p>
<?php endif ?>

(and don’t put the <h3> tag inside the ordered list …)


#3

Thank you so much for the quick answer and good tip! :sweat_smile:


#4

I suggest this for the first statement : <?php if($results->count()): ?>


#5

@ruaideh That does not make sense, because count() returns an integer, not true or false. So if you want to use count, you would have to compare it against an expression

<?php if($results->count() > 0): ?>

#6

Still it works but your code is indeed more correct.


#7

I am having a problem with this. Search results display as expected but if I get zero hits, I don’t get the ‘There are no results’ paragraph.

@texnixe snippet

<?php if($results) : ?>
  <h3>Search results:</h3>
  <ol class="searchresults">
    <?php foreach($results as $result): ?>
      <li>
        <a href="<?php echo $result->url() ?>">
         <?php echo $result->title()->html() ?>
        </a>
      </li>
    <?php endforeach ?>
  </ol>
<?php else : ?>
  <p>There are no results</p>
<?php endif ?>

I tried this example also but I get the same result.

I am using the standard search controller example snippets from here

Any ideas on how to get this to work?

Thanks


#8

Try this:

<?php if($results->count()) : ?>
  <h3>Search results:</h3>
  <ol class="searchresults">
    <?php foreach($results as $result): ?>
      <li>
        <a href="<?php echo $result->url() ?>">
         <?php echo $result->title()->html() ?>
        </a>
      </li>
    <?php endforeach ?>
  </ol>
<?php else : ?>
  <p>There are no results</p>
<?php endif ?>

#9

Thanks! That works. Can you explain why? I am confused because earlier in this post you said using count without comparing it against an expression doesn’t make sense


#10

Then I was being stupid, as “0” is considered false in PHP.

http://php.net/manual/en/types.comparisons.php


#11

Hello

I am using the same code as above and it works perfectly well. What is the best way to display the message below only if the button “Search” is clicked? Now it is visible all the time even if there was no search.

Thanks a lot in advance !


#12
<?php else : ?>
  <?php if($get('q'): ?>
  <p>There are no results</p>
  <?php endif ?>
<?php endif ?>

Or check if $queryis set.
Or you wrap the complete code block within the if statement.


#13

Thanks a lot ! I did it like this:

<?php else: ?>
  <?php if(isset($query)): ?>
    <p>There are no results</p>
  <?php endif ?>
<?php endif ?>