View Products Subcategory

Asked

Viewed 126 times

0

I’m making a virtual store, but I’m not able to display the products of the subcategory on the page.

Category products will be normal, but sub-category products will not.

Follows the code:

<?php 
    $categoria = $_GET['categoria'];
    $categoria_sub = $_GET['categoria_sub'];
    $sql = mysql_query("SELECT * FROM produto
                         INNER JOIN categoria
                            ON produto.categoria = categoria.categoria_id
                         INNER JOIN categoria_sub
                            ON produto.categoria_sub = categoria_sub.sub_cat_id                                                     
                         WHERE produto.categoria = '".$categoria."' 
                           AND produto.categoria_sub = '".$categoria_sub."'
                        ORDER BY RAND()");
    if (mysql_num_rows($sql)== true) {
        while ($ln = mysql_fetch_assoc($sql)) {
?>

Tables:

PRODUCT inserir a descrição da imagem aqui

CATEGORY inserir a descrição da imagem aqui

SUBCATEGORY inserir a descrição da imagem aqui

Show 1 more comment

1 answer

1


If a sub-category belongs to a category, it is not necessary to have one fk for category and one for sub-category in the product register, it is sufficient to have the sub-category. The same applies to WHERE where if you filter by sub-category, automatically the category will be filtered by the condition c.categoria_id = s.sub_cat_categoria.

Having:

$categoria = $_GET['categoria'];
$categoria_sub = $_GET['categoria_sub'];

Follows the code:

SELECT 
* 
FROM produto p
INNER JOIN categoria_sub s on s.sub_cat_id = p.categoria_sub 
INNER JOIN categoria c on c.categoria_id = s.sub_cat_categoria
WHERE p.categoria_sub = '".$categoria_sub."'
ORDER BY RAND()

If you want to filter by category only:

Using the key you have in the product table:

SELECT 
* 
FROM produto p
INNER JOIN categoria_sub s on s.sub_cat_id = p.categoria_sub 
INNER JOIN categoria c on c.categoria_id = s.sub_cat_categoria
WHERE p.categoria = '".$categoria."'
ORDER BY RAND()

Using the related key in the sub-category:

SELECT 
* 
FROM produto p
INNER JOIN categoria_sub s on s.sub_cat_id = p.categoria_sub 
INNER JOIN categoria c on c.categoria_id = s.sub_cat_categoria
WHERE c.categoria_id = '".$categoria."'
ORDER BY RAND()

UPDATE

Using the related key in the sub-category (without filtering) if nothing is passed in the GET category:

$where_categoria = '';

if($categoria != '') {
    $where_categoria = "WHERE c.categoria_id = '".$categoria."'";
}

SELECT 
* 
FROM produto p
INNER JOIN categoria_sub s on s.sub_cat_id = p.categoria_sub 
INNER JOIN categoria c on c.categoria_id = s.sub_cat_categoria
".$where_categoria."
ORDER BY RAND()
  • Good... and I just get the category? $categoria = $_GET['category']; Because when you click on the link with the name of the category will appear all products of the category...

  • edited, see if it suits you

  • how do I get it on the page q do the list.... all results have to come to the product page_list.php if I put one of the options either comes only the category or only the sub

  • 1

    I had never thought to randomize the output of a query, your ORDER BY RAND() opened up horizons for me

  • @Betinhosilva You can simply "ignore" the line if no sub_category or category is entered. I will edit the friend’s response to you.

  • 1

    @Cava I used if... it worked

  • @Betinhosilva I edited, but I needed someone to approve my edition there. That’s why you didn’t receive my edition there.

  • 1

    @Jeffersonquesado also liked this ORDER BY RAND() but in the specific case, I would not use rsrs. Ordering products would certainly be in alphabetical order, or by a rank of quantities sold, something like that

Show 3 more comments

Browser other questions tagged

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