2015-10-27 71 views
0

我有一個問題,我希望有人能幫助我做一個查詢來獲取avarage評價是數據庫表。使用mySQLi avg()函數。我的查詢應該是什麼?

我的數據庫正在建立這樣的:(我希望它夠清楚明白)

數據庫名稱「beoordelingen」

在數據庫中的第一個表是「beoordeling」在那裏我存儲在3列的文件名(文件)和標題(SpeI位):

[ id | file | Spel ] 

在數據庫中的第二個表是 '評論' 3列:

[ id | comment | form_id ] 

在數據庫中的第三個表是「game_rating」 3列:

[ id | form_id | rating ] 

我想這樣做,是回波表game_rating的avarage評級,但我有問題的理解我需要什麼說在查詢來實現這一點。

我創建,所有的遊戲也顯示,並製作成一個鏈接點擊此鏈接,您收到一個ID,並繼續到詳細頁母版頁。 下面是從母版頁的代碼:

<?php 
    //Place all data from this mySQL query in $result. 
    $result = $conn->query("SELECT * FROM beoordeling"); 

    //While a row of data exists, put that row in $data as an associative array. 
    while($data = $result->fetch_assoc()) { 

     //Echo links to all the games in the MySQL database. 
     echo "<a href='detail.php?id=" . $data['ID'] . "'>"; 

      //Echo the games name. 
      echo $data['Spel']; 

     echo "</a>"; 
     echo "<br />"; 
    } 
?> 

這是詳細信息頁面:

<!DOCTYPE html> 
    <?php 
     include("dbconnect.php"); 
     error_reporting(E_ALL); 


     if(isset($_GET['id'])) { 
      $id = (int)$_GET['id']; 

      //Place all data out of the database, with the ID number retrieved out of the url in $result. 
      $game = $conn->query("SELECT * FROM beoordeling WHERE id = '" . $id . "'"); 
     } 

     //While a row of data exists, put that row in $data as an associative array.   
     while($data = $game->fetch_assoc()) { 
      $gameName = $data['Spel']; 
      $fileName = $data['file']; 
     } 

     //CommentList retrieves all comments with id = x. 
     $commentList = $conn->query("SELECT * FROM comments WHERE form_id = '" . $id . "'"); 
    ?> 
<html> 
    <head> 
     <meta charset="UTF-8"> 
     <link rel="stylesheet" type="text/css" href="style.css" media="screen" /> 
     <title><?php echo $gameName; ?></title> 
    </head> 
    <body> 

    <?php   
      /* 
      * 
      * @ToDo: Make point system show avarage rating 
      * 
      * @ToDo: Create a webmaster page where you can add games to the list 
      * and automaticly create game page. 
      * 
      */ 

       //Retrieve the file name from the database and place it in the <embed> tags as src="...". 
       echo "<embed width='800' height='512' src='{$fileName}' type='application/x-shockwave-flash'></embed><br />"; 



     //Echo the form with a text box and a rating box. 
     echo '<div id="game_form"><form method="POST"> 
      <a>Leave a comment</a><br /> 
      <input type="text" name="comments" /> 
      <br /><a>Rate'.$gameName.'</a><br /> 
     <select name="rategame"> 
      <option value="">Select...</option> 
      <option value="1">1</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
      <option value="4">4</option> 
      <option value="5">5</option> 
     </select> 
      <input type="submit" name="submit" value="submit" /> 
     </form></div>'; 

      //Create a table with all the comments 
      echo "<div id='table_data'><table>"; 

      while($cdata = $commentList->fetch_assoc()) { 
       echo "<tr>"; 
       echo "<td>" . $cdata["comment"] . "</td> <br /><br />"; 
       echo "</tr>"; 
      } 
      echo "</table></div>"; 

     //Submit functionality  
     if (isset($_POST['submit'])) { 

     // Check connection 
     if ($conn->connect_error) { 
      die("Connection failed: " . $conn->connect_error); 
     } 

     //Check if comment is entered else set it to an empty string 
     if (isset($_POST['comments'])) { 
      $commentText = $conn->real_escape_string($_POST['comments']); 
     } else { 
      $commentText = ""; 
     } 

     $rate = $_POST['rategame']; 

     echo $rate; 

     //If user enters either rate or comment (or both) send a query to table. 
     //Else only send the query with the users input which the user has entered. 
     if (($rate >= 1) && ($rate <= 5)) { 
      if ($commentText === "") { 
       $sqlrate = "INSERT INTO game_rating (rating, form_id) VALUES ('{$rate}','{$id}')"; 
       $sqlcomment = ""; //Initializing to null to avoid error. 
      } 
      else { 
       $sqlrate = "INSERT INTO game_rating (rating, form_id) VALUES ('{$rate}','{$id}')"; 
       $sqlcomment = "INSERT INTO comments (comment, form_id) VALUES ('{$commentText}','{$id}')"; 
      } 
     } 
     else { 
      if ($commentText !== "") { 
      $sqlcomment = "INSERT INTO comments (comment, form_id) VALUES ('{$commentText}','{$id}')"; 
      $sqlrate = ""; //initializing to null to avoid error. 
      } 
     } 

     //Push query to database 
     if($sqlcomment) { 
      $conn->query($sqlcomment); 
     } 

     if($sqlrate) { 
      $conn->query($sqlrate); 
     } 


     //Close connection to free up resources. 
     $conn->close(); 
     } 
    ?>  
    </body> 
</html> 

所以詳細信息頁面,我想顯示avarage評級的遊戲。 任何人都可以幫助我做到這一點? :D非常感謝!

回答

2

假設在game_rating表form_id是一個外鍵表beoordeling,那麼這可能做的伎倆:

$ratings = $conn->query(
     "SELECT AVG(rating) avg_rating " . 
     "FROM game_rating WHERE form_id = '" . $id . "'"); 
$data = $ratings->fetch_assoc(); 
$avg_rating = $data['avg_rating']; 

SELECT總是會返回一個記錄。如果未找到評分,它可能包含值NULL,但是無論如何您都會有記錄。

+0

感謝這個!有用。我實際上在YouTube上查看了一個用於php評分系統的視頻,當他使用LEFT JOIN加入表格時我非常困惑。我確信我需要這個查詢,所以這實際上是一個緩解,看看它並不是真的有必要。 – Dennis1679

+0

因爲你已經有了'id',所以'join'沒有必要。例如,如果你只有'file'並且希望獲得與該鏈接相關的評分,那麼將會要求加入'join'。 Groetjes。 – trincot

相關問題