How to search all the records in the database?

Asked

Viewed 466 times

-1

Help me out, guys!!!

$BuscaProdutosAtivos = $pdo->prepare("SELECT * FROM tbl_produtos p
                                      INNER JOIN tbl_categorias c ON c.cat_id = p.prod_categoria
                                      INNER JOIN tbl_variacoes v ON v.cod_prod = p.id
                                      WHERE p.prod_ativo = 1");

////////////////////////////////                                      
$BuscaProdutosAtivos->execute(); 
$BuscaProdutosAtivos->rowCount();

I’m searching for 3 types of information within the database: first I search for a product that is active to show in the store, so far so good.

Then I look for the category to which this product belongs and also everything goes well.

My product can be green, yellow or blue and that’s what I look for in the table tbl_variacoes and try to put inside a dropdown and that’s where I have trouble because it returns only the first variation, ie only the first color.

while($PA = $BuscaProdutosAtivos->fetch()){ ?>
    <select name="variacoes" class="form-control chosen-select" data-placeholder="Escolha a variação">
        <option value=""></option>
        <option value="1"><?=$PA['cor']?></option>
    </select>
?>

How to return all variations within the dropdown?

This code returns to me all base products that meet the requirements. If you can help me understand !!!!

inserir a descrição da imagem aqui

  • 1

    Running the query directly in Mysql the expected records are returned?

  • There is no mistake!!! Only in the dropdown list it only takes the first variation. Actually this while is to list all the products, so I think you need to do something else to return the variations. I made an update of the question by placing full code link.

  • The query is returning perfectly what it should return which is all active products with at least one category and one variation. All that remains is to return ALL variations.

  • 1

    Review this JOIN, apparently he’s wrong: INNER JOIN tbl_variacoes v ON v.var_id = p.prod_categoria, you are crossing the ID of the variation with the ID of the category

1 answer

2


With this query, the query will return all the variations registered in tbl_variacoes.

$BuscaProdutosAtivos = $pdo->prepare("SELECT * FROM tbl_produtos p
                                      INNER JOIN tbl_categorias c ON c.cat_id = p.prod_categoria
                                      INNER JOIN tbl_variacoes v ON v.cod_prod = p.id
                                      WHERE p.prod_ativo = 1");

Let’s assume you have 3 colors registered as variations, at the time you run the command while, in the generated result, would perform 3 repetitions. In your current code, will return you 3 boxes of the same product. To prevent this from happening, a small adjustment in the query:

$BuscaProdutosAtivos = $pdo->prepare("SELECT *, GROUP_CONCAT(v.cor) AS cores FROM tbl_produtos p
                                      INNER JOIN tbl_categorias c ON c.cat_id = p.prod_categoria
                                      INNER JOIN tbl_variacoes v ON v.cod_prod = p.id
                                      WHERE p.prod_ativo = 1 GROUP BY p.id");

Note that at the end of the query included the GROUP BY p.id, which will group the result by the product id in tbl_produtos and after the SELECT * included the GROUP_CONCAT(v.cor) AS cores which will concatenate the grouped result of the column v.cor, separating by ,, and defined the nickname as cores.

And in the code you shared in this link, a minor amendment will be necessary:

<select name="variacoes" class="form-control chosen-select" data-placeholder="Escolha a variação">
<option value=""></option>
<?php foreach(explode(',',$PA['cores']) as $cor) : ?>
    <option value="<?=$cor?>"><?=$cor?></option>
<?php endforeach; ?>
</select>
  • Good morning. So the table variations are relating to the table products, ie such product has so many variations. In my code it was understood that variations related to the table categories, which is not true. I modified SQL to make this clear. The answer thus continues to apply to this situation?

  • Yes. However, the table tbl_variacoes you will need to have the spine cod_prod.

Browser other questions tagged

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