简体   繁体   English

评分系统:平均评分

[英]Rating system: Pulling in average rating

the following is my code to do a database search and return the results in this format: 以下是我执行数据库搜索并以这种格式返回结果的代码:
- Institute name 1 - 研究所名称1
Rating: x/5 评分:x / 5
- Institute name 2 - 研究所名称2
Rating: x/5 评分:x / 5

code: 码:

search_results.php search_results.php

<?php
  //mysql_
    $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
    if(mysqli_connect_errno())
    {
        die("Database connection failed: " .
                    mysqli_connect_error() .
        " (" . mysqli_connect_errno() . ")");
    }

  $result = "";
  //collect info from database
  if(isset($_POST['search'])) {
      $searchq = $_POST['search'];
      $searchq = preg_replace("#[^0-9a-z]#i", "", $searchq);
      //echo $searchq;
//SQL query
      $query = "SELECT institute_id, name FROM institutes WHERE 
                                 category1 LIKE '%$searchq%' OR
                                 category2 LIKE '%$searchq%' OR
                                 category3 LIKE '%$searchq%' OR 
                                 category4 LIKE '%$searchq%'";
       $result = mysqli_query($connection, $query);

      // Test if there was a query failure   
      if(!$result){
             die("Database query failed.");
         }                                                     
      $count = mysqli_num_rows($result);
      if($count == 0)
      {
          $output = "There's no search result";
      }
      else {
          while($row = mysqli_fetch_assoc($result))
    {
              $id = $row["institute_id"];
    ?>
              <li>
                  <h3>
                    <a href="institute_profile_test.php?id=<?php echo $id; ?>"><?php echo $row["name"];?>
                    </a>
                  </h3>

              </li>
      <div class = "rating">Rating: x/5</div>
    <?php
    }

}
}
      ?>

I have a ratings and institutes table in my database whose table structures are as follows: 我的数据库中有一个评级和机构表,其表结构如下:
ratings 等级
institutes 研究所

As you can see, the ratings database already stores some rating information.(I will share the code i wrote to register ratings if required) 如您所见,评级数据库已经存储了一些评级信息(如果需要,我将共享我编写的用于注册评级的代码)

What i need to do next is pull in the average rating for each institute and substitute the x with each individual average. 我接下来要做的是获取每个机构的平均评分,并用每个单独的平均值替换x。 What additional code do i write in search_results.php to achieve this goal? 我要在search_results.php中编写哪些附加代码来实现此目标? I would also like to sort the institute names as per their average ratings. 我还想按机构的平均排名对机构名称进行排序。

Calculating/Saving the average 计算/保存平均值

If I understand correctly, all you need to do is create a simple while loop that runs through all the instances of the institute_id being fetched for each successful "search" query. 如果我理解正确,那么您要做的就是创建一个简单的while循环,该循环遍历为每个成功的“搜索”查询所获取的institute_id的所有实例。

while($row = mysqli_fetch_assoc($result)){
    $id = $row["institute_id"];
    $query2 = "SELECT rating FROM ratings WHERE institute_id ='$id'";
    $result2 = mysqli_query($connection, $query2);
    //average rating vars
    $count = 0;
    $sum = 0;
    while($row2 = mysqli_fetch_assoc($result2)){
         $count++;
         $sum += $row2['rating'];
    }
    $average = $sum / $count;
    //display average
    .....
}

That should allow you to display the average for each institute, then if you want to display them according to DESCENDING or ASCENDING just save each average in an array. 这样应该可以显示每个研究所的平均值,然后如果要根据DESCENDING或ASCENDING显示它们,只需将每个平均值保存在数组中即可。 The rest is up to you, try saving the average results in a JSON array and pairing each result with its institute id counter part. 其余的取决于您,请尝试将平均结果保存在JSON数组中,并将每个结果与其机构ID计数器部分配对。

example: 例:

$average = array('institute_x' => 'average')

*Ensure to replace 'institute_x' with id and 'average' with average... *请确保将“ institute_x”替换为id,并将“平均值”替换为平均值...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM