count repeated fields from a column

Asked

Viewed 314 times

-1

As I could count the repeated fields of my table, I tried so more did not work:

Hold the name of the generous

    $generos = mysqli_query($conn,"SELECT * FROM `generos`");

    while($res = mysqli_fetch_assoc($generos)){

            $nomeGenero             = $res['nome_genero'];
            $nomeGeneroCaracteres   = $res['nome_genero'];

            $gn = [rtrim(''.$nomeGenero.'',',')];

            foreach ($gn as $epAtual){

                @$generoAtual = $urlE[1];

                $contaGeneros = mysqli_query($conn,"SELECT * FROM `seriados`");

                while($row = mysqli_fetch_assoc($contaGeneros)){

                    $genero = $row['genero_serie'];

                    $conta = mysqli_num_rows($genero);

                    if($generoAtual == $epAtual){

                        $epAtual = preg_replace('/[-]/ui', ' ',$epAtual);

                            echo '<li><a href="javascript:void(0)" class="ativo">'.utf8_encode($nomeGenero).' <strong>123</strong></a></li>'.PHP_EOL;

                    }else{

                            echo '<li><a href="'.$urlBase.'/genero/'.$nomeGeneroCaracteres.'">'.utf8_encode($nomeGenero).' <strong>123</strong></a></li>'.PHP_EOL;
                    }
            }
    }
}

Goal is to leave so: animes 2 | aventura 5 | comedia 7 .....

hiccup

I got it this way, this one for whoever needs it

$generos = mysqli_query($conn,"SELECT * FROM `generos`");

while($res = mysqli_fetch_assoc($generos)){

    $nomeGenero             = $res['nome_genero'];
    $nomeGeneroCaracteres   = $res['nome_genero'];

    $generoSerie = $res['nome_genero'];

    $seriadosConta = mysqli_query($conn,"SELECT COUNT(genero_serie) AS TOTAL FROM seriados WHERE genero_serie LIKE '%$generoSerie%'");

    while($row = mysqli_fetch_assoc($seriadosConta)){

        $gn = [rtrim(''.$nomeGenero.'',',')];

        foreach ($gn as $epAtual){

        @$generoAtual = $urlE[1];
        if($generoAtual == $epAtual){

        $epAtual = preg_replace('/[-]/ui', ' ',$epAtual);

            echo '<li><a href="javascript:void(0)" class="ativo">'.utf8_encode($nomeGenero).' <strong>'.$row['TOTAL'].'</strong></a></li>'.PHP_EOL;

        }else{
            echo '<li><a href="'.$urlBase.'/genero/'.$nomeGeneroCaracteres.'">'.utf8_encode($nomeGenero).' <strong>'.$row['TOTAL'].'</strong></a></li>'.PHP_EOL;
        }
   }

} }

Source

using mysqli to count records

  • my God.. this is going to waste so much server resource while > foreach > while ??

  • @13dev I need to leave the menu marked according to the page that weighs this, there is another way ?

  • breaks code into functions so you can reuse and take advantage of the performance of PHP

  • @13dev I’m new, I’ve found a big step have come up to here

  • I didn’t question any of that, so take a look at PDO and orientação a objectos, you’re gonna love it! :)

  • If your goal is just to count the amount of genres, do it directly in sql and not by loops bringing your complete query.

  • @Luizpillon would be more or less like this ? SELECT genero_serie FROM seriados, How many fields have the same name directly in sql ? passing the value of it in the url I can count, the problem is when I don’t pass anything url

  • I’ll send the query to Voce in the answers

  • I don’t know why they vote negative if they don’t help anything, leave it to those who want to help

Show 4 more comments

2 answers

1

Hello @Mayron, I’ll put an example for you to see how you can check the repeated values:

<?php

$valores = array(1, 2, 2, 3, 3, 3, 4, 4, 4, 4);
$contagem = array_count_values($valores);

foreach($contagem AS $numero => $vezes) {

    /*aqui no if, vc pode definir qual o valor que deseja realizar a contagem dos repetidos*/
    if($vezes == 4) {
        echo "$numero - $vezes<br />";
    }

}

?>
  • there is no direct account way in sql ?

  • tries as follows: SELECT *, COUNT(gener) AS qtd_nGenero FROM generos GROUP BY gener_name HAVING COUNT(gener) > 1 ORDER BY COUNT(gener) DESC

  • while($res = mysqli_fetch_assoc($generos)){ $qtd_nGenero = $res['qtd_nGenero']; echo $qtd_nGenero; }

  • did not work, he doubled only one field, did the right count, but in the second duplicate he shot a count '5' I do not know where this 5 comes from

1

Follow the query only to count your generos total.

SELECT distinct(nome_genero), count(nome_genero) FROM generos group by nome_genero;

Browser other questions tagged

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