Use explode to separate categories

-1

Good afternoon how could I use the explode in this code snippet so it has more than one category per movie? thanks for the help

$categoria = $_GET['category'];
$sql = "SELECT * FROM filme WHERE categoria IS NOT NULL AND categoria LIKE '%$categoria%' ORDER BY nome LIMIT 25";

variable $categoria it is a get and this is being called in the index in the href com? category = action or if it is not action movies would be in another href? category = adventure

    
asked by anonymous 01.09.2015 / 22:48

3 answers

2

Although what you are doing is subject to SQL Injection, you can use implode() to transform an array to a string.

Assuming your $ _GET looks like this:

http://site.com.br/filmes?category=acao&category=romance&category=thriller

You could do something like this:

$categoria = $_GET['category'];
$likes = implode("%' OR categoria LIKE '%",$categoria);
/* esse implode faz um array assim:
  [0] => 'acao',
  [1] => 'romantico',
  [2] => 'thriller'

  ficar assim:

  "acao%' OR categoria LIKE '%romantico%' OR categoria LIKE '%thriller"
*/

$sql = "SELECT * FROM filme WHERE categoria IS NOT NULL AND ( categoria LIKE '%$likes%' ) ORDER BY nome LIMIT 25";

The explode() takes a string and becomes an array, for example:

$str = "este,e,um,string";
$array = explode(",",$str);
//devovle:
/*
  [0] => "este",
  [1] => "e",
  [2] => "um",
  [3] => "string"
*/

So to use explode() , $ _GET would have to look like this:

http://site.com.br/filmes?category=acao,romance,thriller

And then the only thing that changes is:

$categoria = explode(',', $_GET['category']);

Elaboration

It has been clarified that your problem is that acao also returns animacao due to the same acao .

In this case, you would have to change your SQL. You have a few options:

  • Use IN() to match right. Thus, it returns movies where category is EQUAL "action", but not "animation" or "action-terror".

    SELECT * FROM filme WHERE categoria IN ( 'acao' )

  • Use LIKE , but less % . So, it returns movies where category has "action" in front and not as end of word.

    SELECT * FROM filme WHERE categoria LIKE 'acao%'

  • Here is a SQLFiddle , showing an example of both.

        
    01.09.2015 / 23:04
    1
      

    I suggest that you edit your question, because as you mentioned in one answer, your problem is another.

    My answer to the problem:

    Use this function, called clean , to remove the spaces or traits between the characters:

    function clean($string) {
       $string = str_replace(' ', '', $string); // Substitui os espaços vazios.
       $string = str_replace('-', '', $string); // Substitui os traços 
    
       return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Remove todos os special chars.
    }
    
    echo clean('anim a c a o');
    echo '<br />';
    echo clean('anim-a-c-a-o');
    

    Output:

    animacao
    animacao
    

    EXAMPLE ONLINE

        
    02.09.2015 / 00:51
    1

    Assuming the contents of $_GET['category'] is for example:

    $_GET['category'] = "ação, comédia, romance";
    

    You could do this:

    $categorias = !empty($_GET['category']) ? $_GET['category'] : '';
    
    $categorias = implode("', '", explode(', ', $categorias));
    
    $sql  = "SELECT * 
             FROM filme 
             WHERE 
                categoria IS NOT NULL AND 
                categoria IN ('{$categorias}') 
             ORDER BY nome LIMIT 25";
    

    The% wrapper% is doing the following:

    The $categorias = implode("', '", explode(', ', $categorias)); part explodes the contents of the variable that is explode(', ', $categorias) into an array "ação, comédia, romance" . Then the ["ação", "comédia", "romance"] "paste" content that has been "exploded" in a string using as a separator of each part the string implode("', '", explode(', ', $categorias)) so the result is now% with%.

    With this result we can use the condition ', ' of SQL to say that the field must have a value similar to some of the elements or that is "ação', 'comédia', 'romance" which means that the value of the field IN must be equal the categoria IN ('ação', 'comédia', 'romance') or categoria or ação .

    Or you can still concatenate multiple% s of% s as you were doing:

    $categorias = explode(', ', $categorias);
    
    $sql  = "SELECT * 
             FROM filme 
             WHERE 
                categoria IS NOT NULL AND (";
    
    foreach ($categorias as $categoria) {
        $sql .= " categoria LIKE '%{$categoria}%' OR ";
    }
    
    $sql = rtrim($sql, ' OR ') . ") ORDER BY nome LIMIT 25";
    
        
    01.09.2015 / 23:04