dsj0312 2014-06-14 21:07
浏览 66

投票系统 - 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!

  • 写回答

1条回答 默认 最新

  • dqsw7529 2014-06-14 21:35
    关注

    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!

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路