Search with explode and url friendly (problem)

Asked

Viewed 100 times

1

I have a problem. I was using a search system with multiple variables with no friendly url. And it worked normally. Put several keywords and the same searched in the database without any problem through EXPLODE.

Only now, I put URL friendly and it’s not working. What could it be? Detail: If I put only one word, it will work normally. Now if I put two words does not appear the results.

Search form

    <div class="default-form-area">
            <form id="formBuscar" class="default-form" action="" method="GET">
                <div class="row clearfix">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <div class="form-group">
                            <input type="text" name="search" id="search"  class="form-control" value="<?php if(isset($_GET["search"])) echo $_GET["search"]; ?>" placeholder="Escreva pelo menos uma palavra para realizar a busca... *" required="">
                        </div>
                    </div>
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <div class="form-group">
                           <input type="submit" name="search" class="thm-btn thm-color" value="Buscar" />
                        </div>
                    </div>   
                </div>
            </form>
        </div>

<script>
$(function(){
   $("#formBuscar").submit(function(){
         var search = $("#search").val().toLowerCase();
         search = search.replace( " ", "+" );
         window.location = 'http://localhost/buscar/'+search;
      return false;
   });
});
</script>

Print the search results:

Search result

            <div class="table-responsive">  
                 <table class="table table-bordered">  
                 <?php

                 if(isset($_GET["search"]))  
                 {  
                      $condition = ''; 
                      $query = explode(" ", $_GET["search"]);                     
                      foreach($query as $text)  
                      {  

                      $db->query ('
                                    SELECT noticia_title FROM noticia WHERE noticia_title LIKE "%'.$text.'%" or noticia_content LIKE "%'.$text.'%"
                                    UNION
                                    SELECT evento_id FROM eventos WHERE evento_nome LIKE "%'.$text.'%" or evento_content LIKE "%'.$text.'%" or evento_resumo LIKE "%'.$text.'%"
                                    UNION
                                    SELECT album_id FROM albuns WHERE album_name LIKE "%'.$text.'%" or album_descricao LIKE "%'.$text.'%"  
                        ')->fetchAll();

                        if ($db->rows == 0) {
                            echo "<div class=\"alert info\">Nenhum resultado foi encontrado na sua busca!</div>";
                        } else {
                        if ($db->rows == 1) {
                            echo "<p align=\"center\">Durante sua busca foi encontrado 1 resultado em nosso site.</p><br />";
                        }
                        if ($db->rows > 1) {
                            echo "<p align=\"center\">Durante sua busca foi encontrado $db->rows resultados em nosso site.</p><br />";

                        }
                                }       
                 }

                ?>

Remembering: If I put only one word, it will work normally. Now if I put two words does not appear the results.

1 answer

0


As discussed in the comments, the problem was in the file .htaccess that was like:

RewriteRule ^buscar\/([a-z0-9-]+)/?$ index.php?p=buscar&search=$1 [QSA]

The regex used was not marrying other characters passed in GET, but only letters from a to z, numbers and hyphen.

Changing the regex to:

RewriteRule ^buscar\/([\w\W]+)/?$ index.php?p=buscar&search=$1 [QSA]

The problem has been solved because now starts to marry any character passed in GET.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.