1 (edited by legmet 2016-10-11 14:35:56)

Topic: Search blog's post content

use this snippet if you want search blog's post by content or title


<form method="post" class="form-horizontal col-md-12" action="">
<div class="form-group">
  <div class="input-group">
    <input type="text" name="name" class="form-control" placeholder="search by title ...">
    <span class="input-group-btn">
      <button class="btn btn-default btn-link" type="submit" name="submit"><i class="fa fa-search"></i></button>
    </span>
  </div>
</div>
</form>
<?php $pages = Page::children('blog'); ?>
<?php 
$name = strtolower($_POST['name']);
if(isset($_POST['submit'])) 
{
    $counter = 0;
       echo '<p>results for <b>' .$_POST['name']. '</b> :</p>';
       echo '<ul class="list-unstyled">';
    foreach($pages as $page) 
    {   $content = strtolower(Pages::content($page['slug']));
          $title = strtolower($page['title']);   
        if (strpos($title , $name) !== false OR strpos($content , $name) !== false)  {
            echo '<li>';
            echo '<a href="blog/'.$page['slug'].'">' .$page['title']. '</a>';
            echo '</li>';    
            $counter++;
        }    
     }
    echo '</ul>';
    if ($counter === 0) {
        echo '<p><i>no matches found</i></p>';
    }
    else {
        echo '<p>found ' .$counter. '</p>';
    }
}
?>

Re: Search blog's post content

Are you sure this is working?

It does not seem to work on my blog (http://barichello.coffee/blog).

Try "attention", for example. It is in the content on the body of the first post but the script returns 0 matches.

barichello's Website

3 2016-10-13 12:22:07

Re: Search blog's post content

you are right it does not return searching phrase

this line: $content = strtolower(Pages::content($page['slug'])); should return page content

i will think about it

4 2016-10-13 12:24:13

Re: Search blog's post content

barichello wrote:

Are you sure this is working?

It does not seem to work on my blog (http://barichello.coffee/blog).

Try "attention", for example. It is in the content on the body of the first post but the script returns 0 matches.


are you sure you have copied above code ?
it works for me

5 2016-10-13 12:27:41

Re: Search blog's post content

try change:

$content = strtolower(Pages::content($page['slug']));

to :

$content = strtolower(Page::content($page['slug']));


Pages to Page

6 2016-10-18 17:12:57

Re: Search blog's post content

Thank you! smile

barichello's Website

Re: Search blog's post content

it works I guess wink