gianpts
gianpts

Reputation: 11

Vote system - PHP & Mysql


I'm trying to write a script to vote a film, based on "Like/dislike" system of FB. I created a php page where the script show the list of most liked films, taking the data from the "votes" table and with a "while" I would select them from the "films" table , but I have a problem: the output HTML show only one film (the film with the highest id), and the other films with a vote are not showed.
The code of the script is:

if ($_GET['top_rated'] == 'show') {
   $vote= 1; //vote positive(like)
   $sql = $pdo->prepare("SELECT * FROM votes WHERE vote= :vote GROUP BY film_id");
   $swl->bindParam(":vote", $vote)
   $sql->execute();
   while ($vote_info = $sql->fetch(PDO::FETCH_ASSOC)) {
   $film_id = $vote_info['film_id'];
   $result = $pdo->prepare("SELECT * FROM films WHERE id= :film_id");
   $result->bindParam(":film_id", $film_id);
   $result->execute();
}
}

while ($film_info = $result->fetch(PDO::FETCH_ASSOC)) {
        $genre_id = $film_info['genre_id'];
        $sql = $pdo->prepare("SELECT * FROM genres WHERE id=:genre_id");
        $sql->bindParam(":genre_id", $genre_id);
        $sql->execute(); 
        $genre_info = $sql->fetch(PDO::FETCH_ASSOC);
 ?>
<div id="film-home-box">
<img class="poster" src="<?php echo $film_info['film_poster_src']; ?>"></img>
<div class="film-home-info">
Title: <?php echo $film_info['film_name'];?> <br>
Year: <?php echo $film_info['film_year'];?><br>
Genre: <?php echo $genre_info['genre_name'];?>
</div>
</div>

The database's map is:

my_database:
  films->id/film_name/film_year/genre_id 
  genres->id/genre_name
  votes->id/film_id/vote

Thanks in advance!

Upvotes: 1

Views: 394

Answers (1)

Amanuel Nega
Amanuel Nega

Reputation: 1977

That is because your code that displays the film runs once!

You should use some kind of loop to print multiple items. My suggestion is to change

while ($film_info = $result->fetch(PDO::FETCH_ASSOC)) {
        $genre_id = $film_info['genre_id'];
        $sql = $pdo->prepare("SELECT * FROM genres WHERE id=:genre_id");
        $sql->bindParam(":genre_id", $genre_id);
        $sql->execute(); 
        $genre_info = $sql->fetch(PDO::FETCH_ASSOC);
 ?>
<div id="film-home-box">
<img class="poster" src="<?php echo $film_info['film_poster_src']; ?>"></img>
<div class="film-home-info">
Title: <?php echo $film_info['film_name'];?> <br>
Year: <?php echo $film_info['film_year'];?><br>
Genre: <?php echo $genre_info['genre_name'];?>
</div>
</div>

to the following

?><!-- Close the previous php block-->
<div id="film-home-box">
<img class="poster" src="<?php echo $film_info['film_poster_src']; ?>"></img>
<div class="film-home-info">
<?php
while ($film_info = $result->fetch(PDO::FETCH_ASSOC)) {
        $genre_id = $film_info['genre_id'];
        $sql = $pdo->prepare("SELECT * FROM genres WHERE id=:genre_id");
        $sql->bindParam(":genre_id", $genre_id);
        $sql->execute(); 
        $genre_info = $sql->fetch(PDO::FETCH_ASSOC);
        //print the details at each loop!
        //this should work if you have multiple items match your query...
        echo  "Title:  $film_info['film_name'] <br>
        Year: $film_info['film_year'] <br>
        Genre:  $genre_info['genre_name']";
 ?>
</div>
</div>

try it and report what you find out!

Upvotes: 1

Related Questions