Selection of Recipes by Specific Ingredients

Asked

Viewed 25 times

0

Well, I’m creating a recipes website and one of the searches is a search for selected ingredients. My table in the database is this:

CREATE TABLE Receita(
  idReceita INT NOT NULL auto_increment,
  nome_Receita VARCHAR(45) NOT NULL,
  preparo  TEXT NOT NULL,
  ingredientes text not null,
  usuario_idUsuario int,
  categoria_idCategoria int,
  dificuldade_idDificuldade int,
  PRIMARY KEY (idReceita)
); 

And my ingredients are comma separated. My html search page looks like this:

 <div class="container" style="margin-top:60px">
            <div class="col-sm-6" style="//background-color:lavenderblush;">
                <label><h4>Digite os ingredientes que você tem em casa e nós selecionaremos as receitas que mais se enquadram na sua busca.</h4></label><br>
                <form action='processa.php' method="post" accept-charset="UTF-8">
                    <div class="form-group">
                        <input type="text" placeholder="Ingrediente 1" name="text1" class="form-control" id="text1">
                    </div>
                    <div class="form-group">
                        <input type="text" placeholder="Ingrediente 2" name="text2" class="form-control" id="text2">
                    </div>
                    <div class="form-group">
                        <input type="text" placeholder="Ingrediente 3" name="text3" class="form-control" id="text3">
                    </div>
                    <div class="form-group">
                        <input type="text" placeholder="Ingrediente 4" name="text4" class="form-control" id="text4">
                    </div>
                    <div class="form-group">
                        <input type="text" placeholder="Ingrediente 5" name="text5" class="form-control" id="text5">
                    </div>
                    <div class="form-group">
                        <input type="text" placeholder="Ingrediente 6" name="text6" class="form-control" id="text6">
                    </div>
                    <div class="form-group">
                        <input type="text" placeholder="Ingrediente 7" name="text7" class="form-control" id="text7">
                    </div>
                    <div class="form-group">
                        <input type="text" placeholder="Ingrediente 8" name="text8" class="form-control" id="text8">
                    </div>
                    <div class="form-group">
                        <input type="text" placeholder="Ingrediente 9" name="text9" class="form-control" id="text9">
                    </div>
                    <div class="form-group">
                        <input type="text" placeholder="Ingrediente 10" name="text10" class="form-control" id="text10">
                    </div>

                    <button type="submit" id='b' class="btn btn-default">Procurar</button>
                </form> 
            </div> 

My idea is to take the values entered on the page:

<?php
            include "config.php";

            $i1 = $_POST["text1"];
            $i2 = $_POST["text2"];
            $i3 = $_POST["text3"];
            $i4 = $_POST["text4"];
            $i5 = $_POST["text5"];
            $i6 = $_POST["text6"];
            $i7 = $_POST["text7"];
            $i8 = $_POST["text8"];
            $i9 = $_POST["text9"];
            $i10 = $_POST["text10"];

And I think about making one explode in the ingredients of each recipe and making an if with each array index but I don’t know if that’s right. Could someone help me?

  • 1

    "And my ingredients are comma-separated." That seems inefficient to me. You can create two tables (like tabela_ingredientes and another as receita_ingredientes). Thus the receita_ingredientes would have a relationship between the receita and the ingredientes, which may be more than one (a relation M:N, https://stackoverflow.com/questions/43990459/how-to-create-tables-with-nm-relationship-in-mysql). So just give a INNER JOIN. Already in HTML/CSS you can use array or use Variable variables, in order to avoid repairing.

1 answer

-1

It is not the most indicated mode but by following your code there, you can use LIKE and AND for your variables.

$sql = "
    SELECT * FROM Receita WHERE 
        ingredientes LIKE '%{$i1}%' AND
        ingredientes LIKE '%{$i2}%' AND
        ingredientes LIKE '%{$i3}%' AND
        ingredientes LIKE '%{$i4}%' AND
        ingredientes LIKE '%{$i5}%' AND
        ingredientes LIKE '%{$i6}%' AND
        ingredientes LIKE '%{$i7}%' AND
        ingredientes LIKE '%{$i8}%' AND
        ingredientes LIKE '%{$i9}%' AND
        ingredientes LIKE '%{$i10}%'
";

Browser other questions tagged

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