questions about category and subcategory

-1

Good afternoon I created a system of category of products in the store that I am putting in 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 able to do list it so much in one category as in the other. if somebody can help thanks, I'll leave the part of the index that calls the get of the category page and the page of the category obs: in my database I created a field in the table called category and another called subcategory.

part of the index that calls get from the category category:

<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 for sports and running suppose I have a product that are of these two categories I registered in the running category and the sport subcategory as I could do for it to list the msm product in the two categories if I am doing wrong please explain me in a simple and effective way or if it is not that way I appreciate 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 thanks.

    
asked by anonymous 31.08.2015 / 21:25

1 answer

2

I do not know how you did to link products to categories, but since each product can have several categories, it is advisable to create an auxiliary table just to make that connection. 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, you 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'
    
01.09.2015 / 05:17