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?
"And my ingredients are comma-separated." That seems inefficient to me. You can create two tables (like
tabela_ingredientes
and another asreceita_ingredientes
). Thus thereceita_ingredientes
would have a relationship between thereceita
and theingredientes
, 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 aINNER JOIN
. Already in HTML/CSS you can usearray
or useVariable variables
, in order to avoid repairing.– Inkeliz