Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8022005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:08:54+00:00 2026-06-04T22:08:54+00:00

Current situation I have two tables in my database, one for posts, and one

  • 0

Current situation

I have two tables in my database, one for posts, and one for ratings. These are linked with a relation in the MySQL so that one post may have 0, 1 or multiple ratings, but one rating can only be applied to one post.

When I fetch a list of posts, I also want to get ratings, but without having to make a separate call to the database for each post in the foreach loop.

To do this I have attempted to use an SQL query to fetch all posts with a LEFT JOIN on ratings so that it will return a result like this:

statusId|statusBody|rating
-----------------------------
1, post1, 0
1, post1, 1
2, post2, 0
3, post3, 1
3, post3, 1

The SQL works fine, and I get the data I ask for.

Ideally what I am trying to achieve now is to turn this table into a collection of objects, with each object storing the post information as well as a value depending on it’s total ratings.

After using PDO to return the data result, this is the code I am using to map the data:

Code Logic

The logic of my code goes like this:

Get all statuses joined with ratings table

Create empty output array

Loop through PDO result
{
    Create loop specific temp array

    Push first row of result into temp array

    Remove row from PDO result

    Loop through PDO result for objects with matching statusId
    {
        If row matches statusId, add to temp buffer and remove from PDO result
    }

    Take first row of buffer and create status object

    Loop through objects in temp array to calculate ratings and add onto above status object
    Clear temp buffer

    Add status object to output array
}

return output array

Actual Code

  try 
  {
     $result = $pdo->query($sql);
     //if($result == false) return false;
     $statuses = $result->fetchAll(PDO::FETCH_CLASS, 'status');
  } 
  catch (PDOException $e) 
  {
     return FALSE;
  }

  if (!$result) {
     return FALSE;
  }

  //create empty output array to be filled up
  $status_output = array();

  //loop through all status
  foreach($statuses as $s1key => $s1value)
  {     
     //initialise temporary array;
     $status_temp_buffer = array();

     //create temp array for storing status with same ID in and add first row
     array_push($status_temp_buffer, $s1value);

     //remove from primary array
     unset($statuses[$s1key]);

     //loop through array for matching entries
     foreach($statuses as $s2key => $s2value)
     {
        //if statusId matches original, add to array;
        if($s2value->statusId == $s1value->statusId)
        {
           //add status to temp array
           array_push($status_temp_buffer, $s2value);

           //remove from primary array
           unset($statuses[$s2key]);
        }

        //stop foreach if statusId can no longer be found
        break;
     }

     //create new status object from data;
     $statObj = $status_temp_buffer[0];

     //loop through temp array to get all ratings
     foreach($status_temp_buffer as $sr)
     {
        //check if status has a rating
        if($sr->rating != NULL)
        {
           //if rating is positive...
           if($sr->rating == 1)
           {
              //add one point to positive ratings
              $statObj->totalPositiveRatings++;
           }

           //regardless add one point to total ratings
           $statObj->totalAllRatings++;
        }
     }

     //clear temporary array
     $status_temp_buffer = NULL;

     //add object to output array
     array_push($status_output, $statObj);
  }

Problem

The problem I am coming up against with this code is that although the ratings are fine, and it correctly calculates the ratings total for each post, it still shows duplicates where a post has more than one rating.

Any help with this would be greatly appreciated,
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:08:57+00:00Added an answer on June 4, 2026 at 10:08 pm

    As i understood it, the goal is to get the total rating of each Post entry. Instead of manually looping over each and every rating, there are two other path you could take:

    • compute the total in the query:

      SELECT SUM(rating) AS total , .. FROM Posts LEFT JOIN .... GROUP BY statusID
      

      You will receive a list of Post entries, each already with total rating calculated. This is a very good solution if you have a lot of writes to to the Ratings table, and much less reads.

    • the other way is to break the table normalization, but to increase read performance. What you would have to do is to add another column in the Posts table: total_rating. And have an TRIGGER on INSERT in the Ratings table, which changes the Posts.total_rating accordingly.

      This way has a benefit of simplifying the request of Posts. At the same time Ratings table can now be use to ensure that total_rating has been calculated correctly, or to recalculate the value, if there are some large changes in the ratings: like banning of user, which results in removing all ratings made by this user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my current situation: I have two tm structs, both set to the
We have one perplexity about current situation with TFS. There are 3 branches: Develop,
My database has two tables, Reads and Alarms . There is a one to
let me explain my current situation i have a SharePoint site lets say it
My current situation is: I have to read a file and put the contents
The current situation is that topics are sorted by 3 main categories. There is
I am using Asp.Net/C# in my application.My current situation is that I am using
We've run into an interesting situation that needs solving, and my searches have turned
I have two activities one displaying map view and another listview, the main objective
I have a rather classic UI situation - two ListBoxes named SelectedItems and AvailableItems

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.