questions on category and subcategory

Asked

Viewed 194 times

-1

good afternoon I created a product category system in the store I am riding on it I have a page that calls for get the categories but I have a problem I have more than one category per product and I am not managing to list it both in one category and in the other. if anyone can help I’ll leave the part of the index that calls the category page get and the category Obs page: in my database I created a field in the table called category and another called subcategory.

part of the index calling the get of the category page:

<div class="col-md-6">
                <div class="thumbnail">
                    <a href="category.php?category=esportes"><img src="images/prod1.jpg" alt="esportes" class="acende" title="esportes"></a>
                    <span class="esporte">Esportes</span>
                </div>
            </div>
            <div class="col-md-3">
                <div class="thumbnail">
                    <a href="category.php?category=running"><img src="images/run_prod.jpg" class="acende" title="running"></a>
                    <span class="resto">Running</span>
                </div>
            </div>

in this example I have a category listing by sports and running suppose-if I have a product that are of these two categories I have registered in the running category and in the sport subcategory how I could make it to list the msm product in the two categories if I am doing wrong please explain to me in a simple and effective way or if it is not this way I graduate any information.

now the category page

<?php

    $categoria = $_GET['category'];
                $quantidade = 24;// qunatidade que ira aoparecer por pagina
                $pagina = (isset($_GET['pagina'])) ? (int)$_GET['pagina'] : 1;
                $inicio = ($quantidade * $pagina) - $quantidade;
                $sql = "SELECT * FROM filme WHERE categoria = '$categoria' ORDER BY nome LIMIT $inicio, $quantidade";
    ?>

here are my codes if anyone can help us.

  • The right is for you to pass the category ID and not the name in the URL, because of the special characters you may have. Category.php? idCategory=56

  • still in case I would have to take the Where category and pass as Where id = '$category' if that’s the case as I would show in the index intact in the url call? how I would call the id on her?

  • You can list all categories and link to them you do: page.php? idCategoria=<? echo $Row['idCategoria']; ? >, with this you can search the data that are within this category, whether product, text or any type.

  • suponha-se que eu tenha um produto que sao dessas duas categorias. Perhaps the right thing to do is to keep the categories in a field VARCHAR separated by commas, and when you filter the categories, make a explode to process all listed categories, although this is somewhat laborious for the database. But store ID for different categories would be complicated and even more laborious, and perhaps you would have to resort to the same technique only this time using inteiros, the more correct would be imagine it as tags. It will be something like this ?

  • good morning intao cara I tried to make it use explodes but gave a problem first vo show the code I did now in it has a simple error: $sql = "SELECT * FROM filme WHERE categoria IS NOT NULL AND categoria LIKE '%$categoria%' ORDER BY nome LIMIT $inicio, $quantidade"; in it I have a category with similar names he lists her in the two without I have listed even I give a category only for each ai tried to use the explode until I got but I had a problem also he picked up the attribute after the comma or be the first category not picked up, this and my mistake if I can help.

1 answer

1

I don’t know how you connected the products to the categories, but since each product can have several categories, we recommend creating an auxiliary table just to make this link. It would look like this:

prod_categoria
================================
produto        | categoria
id do produto  | id da categoria

In this case, to call the products belonging to the desired category, could do so:

select 
    p.* 
from categoria c
left join prod_categoria pc on c.id = pc.categoria
inner join produto p on p.id = pc.produto
where 
    c.descricao = 'descricao_da_categoria'

Browser other questions tagged

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