How to correctly count products?

Asked

Viewed 63 times

0

I need to do a product count to show in front of the subfilter that I have on a page, but as much as I try the values are getting incorrect, I thought it could be the way I’m showing the records, with DO and WHILE, I ended up doing the inversion for WHILE and DO, Take this example, the first filter:

Espelho 
  c/espelho (02)

You can check here: Example of Counting:

I actually have 3 mirror products, but my count initially takes 2, the code that shows the filter, subfilter and the count is this:

// VERIFICA SE TEM DEPARTAMENTO E SUBDEPARTAMENTO
if ($dep != ""  and $sub != "") {   

// ARRAY PARA GUARDAR VARIÁVEIS
$arrayProdutos = "";
$arrayFiltros = "";

// COMEÇANDO O MENU
while($row_rsPesquisa = mysql_fetch_assoc($rsPesquisa)) {               

    if ($arrayProdutos == "") { 
        $arrayProdutos = $row_rsPesquisa['id_produto'];                     
    } else {
        $arrayProdutos = $arrayProdutos . "," .$row_rsPesquisa['id_produto'];
        // print_r($arrayProdutos);
    }

}

    mysql_select_db($database_conexao, $conexao);
    $query_filtro = "SELECT 
                          filtro.id_filtro,
                          filtro.descricao
                        FROM
                          produtos_filtro_subfiltro
                          INNER JOIN filtro ON (produtos_filtro_subfiltro.id_filtro = filtro.id_filtro)
                        WHERE
                          produtos_filtro_subfiltro.id_produto IN ($arrayProdutos)
                          GROUP BY
                          produtos_filtro_subfiltro.id_filtro";
    $filtro = mysql_query($query_filtro, $conexao) or die(mysql_error());
    $row_filtro = mysql_fetch_assoc($filtro);                                           
    $totalRows_filtro = mysql_num_rows($filtro);

// MOSTRANDO MENU
do {

    $IdFiltro = $row_filtro['id_filtro'];

    echo 
      "<ul class='menu'>
        <li><a href='#'>$row_filtro[descricao]</a></li>
        <li class='current-menu-item'></li>                
      </ul>";   
    // FIM DO MENU                  

        // COMEÇANDO BUSCA DE DADOS PARA SUBMENU
        mysql_select_db($database_conexao, $conexao);
        $query_rsSubFiltro = "SELECT
                              COUNT(*) AS registro,  
                              subfiltro.descricao,
                              subfiltro.id_subfiltro
                            FROM
                              produtos_filtro_subfiltro
                              INNER JOIN subfiltro ON (produtos_filtro_subfiltro.id_subfiltro = subfiltro.id_subfiltro)
                            WHERE
                              produtos_filtro_subfiltro.id_filtro = $IdFiltro AND 
                              produtos_filtro_subfiltro.id_produto IN ($arrayProdutos)
                            GROUP BY
                              subfiltro.descricao";
                              // echo $query_rsSubFiltro;
        $rsSubFiltro = mysql_query($query_rsSubFiltro, $conexao) or die(mysql_error());
        $row_rsSubFiltro = mysql_fetch_assoc($rsSubFiltro);
        $totalRows_rsSubFiltro = mysql_num_rows($rsSubFiltro);

        do {
            // RESGATANDO O FILTRO PARA COMPOR A URL DE BUSCA
            $somaFiltro = $row_rsSubFiltro['registro'];
            $IdSubFiltro = $row_rsSubFiltro['id_subfiltro'];
            $descProdutoSub = $row_rsSubFiltro['descricao'];
        echo 
          "<ul class='menu2'>
            <li><a href='resultado.php?dep=$dep&sub=$sub&filtro=$IdFiltro&subfiltro=$IdSubFiltro'>  $descProdutoSub ($somaFiltro)</a></li>
          </ul>";
        } while($row_rsSubFiltro = mysql_fetch_assoc($rsSubFiltro));

        // FIM DO SUBMENU
} while($row_filtro = mysql_fetch_assoc($filtro)); // FIM DO WHILE DO MENU

// echo $arrayFiltros;
} // FIM DO IF
?>
  • 1

    Friend, your code and your example have SQL Injection fault

  • Thanks for the tip @Isvaldofernandes, already fixed it, now the problem of counting, not yet.

No answers

Browser other questions tagged

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