How to use tags in Mysql and PHP

Asked

Viewed 493 times

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.

  • 2

    I advise you to create a table for tags and then associate tags.

  • 2

    It is not easier to make a 1:n relationship, instead of separating by commas?

  • And if you have more than one tag?

  • 1

    products => [id, name] | information => [text, tag_id] | tag => [id, name]

  • 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

  • Post the DER of your preferred bank, so we can understand.

  • 1

    I’ll tell you what: give explode in comma to get the parts is a lady gambiarra. That’s why we recommend the relationship!

  • And if I have multiple tags for the same information?

  • Márcio, this is precisely why the relationship 1:n . . . in other words, 1 information can have none or many tags

  • 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?

  • 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.

Show 6 more comments
No answers

Browser other questions tagged

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