Count Out of a Ranking

Asked

Viewed 37 times

0

I have a movie ranking system on my website and I need it NAY show how many users 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>';
    }
}

Esse é o ranking, preciso tirar esses números daí

  • To solve this, just put index in value and show only values with the indexes you want. in case, $field is your index...

2 answers

0


Option 1:

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

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

So you’ll only present the names of the films.

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, no need for both.

  • Continue can get confused when debugging time. I would trade for if($field == 'filmes') echo '...'.

  • I can only test the night code, but could you explain to me why it would take the numbers off? I’d like to learn this.

0

If I understood the question I would ask differently: I would use the assoc() to associate the columns and print only the column with the name of the movies, sql will already do the 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>';
}

Browser other questions tagged

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