Take the count of a ranking

0

I have a movie ranking system on my site and I need it NOT to show how many users have recommended such a movie, here's my code:

$query = "SELECT DISTINCT filmes, count(filmes) FROM filmes_rec 
          GROUP by filmes
          ORDER by count(filmes) DESC LIMIT 10";

$result = mysqli_query($link, $query);

while($row = mysqli_fetch_row($result)) {
    foreach($row as $field => $value) {
        echo '<hr class="break"><tr><p class="title">' . htmlspecialchars($value) . '</p></tr>';
    }
}

    
asked by anonymous 14.10.2016 / 03:56

2 answers

0

Option 1:

I will disregard that you make use of the result of your query after the loop then change it to:

$query = "SELECT DISTINCT filmes FROM filmes_rec 
          GROUP by filmes
          ORDER by count(filmes) DESC LIMIT 10";

So you're just going to present the names of the movies.

Option 2:

Change this code snippet:

while($row = mysqli_fetch_row($result)) {
    foreach($row as $field => $value) {
        if ($field != 'filmes') continue;
        echo '<hr class="break"><tr><p class="title">' . htmlspecialchars($value) . '</p></tr>';
    }
}

Obs: Use either option, you do not need both.

    
14.10.2016 / 06:33
0

If I understand the question I would do otherwise: I would use assoc() to associate the columns and print only the column with the name of the movies, sql will already sort:

PHP

$sql = "SELECT DISTINCT filmes, count(filmes) FROM filmes_rec 
      GROUP by filmes
      ORDER by count(filmes) DESC LIMIT 10";

$result = $link->query($sql);

if($result->num_rows() >0){

  while($row = $result->fetch_assoc()){
    echo '<hr class="break"><tr><p class="title">' .$row['filmes']. '</p></tr>';
  }

}else{
 echo '<p>Sem Resultados</p>';
}
    
14.10.2016 / 19:12