2
I am creating a website and would like to know how I can use tags with Mysql and PHP.
I have two tables: produtos
and informacao
.
On the table informacao
I have a field called related products where I’ll put the products separated by commas, how can I then take those names without the commas so I can do a search with each of them?
I have the following code:
<?
$explode = explode(',',$relacionados);
foreach($explode as $nome){
$result = $connection -> query("select * from produtos where titulo='$nome' and linguagem='$lang'");
while($row = $result -> fetch_array(MYSQLI_ASSOC)){
$id = $row['id'];
$titulo = $row['titulo'];
$resumo = $row['resumo'];
$imagem = $row['imagem'];
$categoria = $row['tags'];
?>
<div class="grid_3">
<div class="box2 wrap1 wow fadeInLeft" data-wow-delay="0.1s">
<a href="verproduto.php?id=<?=$id?>&lang=<?=$lang?>" id="produto"><img class="first" src="<?=$imagem?>" alt=""/></a>
<div class="caption bggreen equal">
<h6 class="text_3 colorblue">
<a href="verproduto.php?id=<?=$id?>&lang=<?=$lang?>"><?=$titulo?></a>
</h6>
<br>
<p class="colorwhite">
<?=$resumo?>
</p>
</div>
</div>
</div>
<?
}}
?>
</div>
<?
$result -> free();
?>
With this code I could only print the second related product which was where the string did the explode
, need to print all products and not where it leaves.
I advise you to create a table for tags and then associate tags.
– Jorge B.
It is not easier to make a 1:n relationship, instead of separating by commas?
– Wallace Maxters
And if you have more than one tag?
– Márcio André
products => [id, name] | information => [text, tag_id] | tag => [id, name]
– Wallace Maxters
So far so good, now I in the information need to appear the related products or need to pick up the tags and search products with title=tag
– Márcio André
Post the DER of your preferred bank, so we can understand.
– thiagobarradas
I’ll tell you what: give
explode
in comma to get the parts is a lady gambiarra. That’s why we recommend the relationship!– Wallace Maxters
And if I have multiple tags for the same information?
– Márcio André
Márcio, this is precisely why the relationship 1:n . . . in other words, 1 information can have none or many tags
– MarceloBoni
I have already done this, I am now with another question. I don’t know if I can put this topic. How can I insert data in mysql from checkboxes? But I want to insert a line for each checkbox selected, ie if you select 4 makes 4 Inserts. It is possible?
– Márcio André
If you solved it, post an answer to your solution and validate it after two days. If you have another problem, create a new question.
– Rodrigo Rigotti