简体   繁体   中英

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
Rating: x/5
- Institute name 2
Rating: x/5

code:

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. What additional code do i write in search_results.php to achieve this goal? 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($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. 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.

example:

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

*Ensure to replace 'institute_x' with id and 'average' with average...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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