Show Quantity of items per category in virtual store?

Asked

Viewed 182 times

1

I’m having a doubt:

I have several categories on my site and would like you to show the amount of items you have in each category. Example:

It’s like this:

  • Accessories
  • Toys
  • Games
  • Informatics

I want you to stay like this:

  • Accessories (3)
  • Toys (1)
  • Games (2)
  • Informatics (0)

How to do this without exceeding the Query limit via PDO?

  • What is this query limit?

  • 2

    Add a piece of your code so someone can create a solution. Tour the site - http://answall.com/tour - and understand how to ask questions.

3 answers

1

Use Count() and group by.

Your query should look like this:

select cat.nome, count(prod.id) as contagem
from categoria as cat
  inner join produto as p on p.id_categoria = cat.id
group by cat.id

0

In this case I have to make a Function? The information of the items are only in a table ("PRODUCTS"), are only in different columns.

It would look something like this?

/***************************************************/
Function qtdCat($cat)
{
$quantity = select cat.name, Count(Prod.id) WHERE category = '$cat' count from category as cat Inner Join product as p on p.id_categoria = cat.id group by cat.id

Return $quantity;
}

Acessórios   <?=qtdCat("aces");?>
Brinquedos   <?=qtdCat("brinq");?>
Games        <?=qtdCat("games");?>
Informática  <?=qtdCat("info");?>

/***************************************************/

0

I solved the problem as described below:

<!--=============================================================-->
<!--===Inicio - Essa Parte trata a quantidade por Categoria======-->

    <?php
    $categoria = array(); //Declarado um Array sem Tamanho
    $contitensCat = 0;    //Essa Variavel vai contar quantas categorias tem Geral
    $itensCat = 0;        //Essa vai entrar na function e contar a quantidade de cada categoria de quem esta chamando a function

            $buscaSegura = $pdo->prepare("SELECT categoria FROM produtos"); //Faz a Busca da coluna inteira da Categoria do banco de dados
            $buscaSegura->execute(); // Executa o Select
            $consultaBanco = $buscaSegura->fetchAll(PDO::FETCH_ASSOC);

    foreach($consultaBanco as $consulta) 
    {
        array_push($categoria, $consulta['categoria']);
        $contitensCat++; //A cada linha Encontrada essa variavel vai ser incrementada
    }


function cont($palavra,$categoria,$contitensCat,$itensCat)
{
    for($i = 0; $i < $contitensCat; $i++)
    {
        if($categoria[$i]==$palavra)//Se achar a palavra, ai entra nesse IF
        {
            $itensCat++;
        }
    }
    return $itensCat;//Retorna  Quantidade de itens achado de acordo com o parâmetro passado a função
}
    ?>

<!--======Fim - Essa Parte trata a quantidade por Categoria======-->
<!--=============================================================-->

 - Acessórios (<?=cont("ac",$categoria,$contitensCat,$itensCat);?>)
 - Brinquedos  (<?=cont("brinq",$categoria,$contitensCat,$itensCat);?>)
 - Games (<?=cont("games",$categoria,$contitensCat,$itensCat);?>)
 - Informática (<?=cont("inf",$categoria,$contitensCat,$itensCat);?>)

Browser other questions tagged

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