Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing users who liked an item in an item list

This is an issue that I've deemed impractical to implement but I would like to get some feedback to confirm.

I have a product and users database, where users can like products, the like data is stored in a reference table with just pid and uid.

The client request is to show 3 users who have liked every product in the product listing.

The problem is, its not possible to get this data in one query for the product listing,

How I once implemented and subsequently un-implemented it was to perform a request for the users who have liked the products during the loop through the product list.

ie.

foreach($prods as $row):
$likers = $this->model->get_likers($row->id);
endforeach;

That works, but obviously results in not only super slow product listings, and also creates a big strain on the database/cpu.

The final solution that was implemented was to only show the latest user who has liked it (this can be gotten from a join in the products list query) and have a link showing how many people have liked, and upon clicking on it, opens a ajax list of likers.

So my question is, is there actually a technique to show likers on the product list, or is it simply not possible to execute practically? I notice actually for most social media sites, they do not show all likers on the listings, and do employ the 'click to see likers' method. However, they do show comments per items on the listing, and this is actually involves the same problem doesn't it?

Edit: mock up attached on the desired outcome. there would be 30 products per page.

enter image description here

like image 632
Winterain Avatar asked Feb 03 '26 07:02

Winterain


1 Answers

By reading your comment reply to Alex.Ritna ,yes you can get the x no. of results with per group ,using GROUP_CONCAT() and the SUBSTRING_INDEX() it will show the likers seperated by comma or whatever separator you specified in the query (i have used ||).ORDER BY clause can be used in group_concat function.As there is no schema information is available so i assume you have one product table one user table and a junction table that maintains the relation of user and product.In the substring function i have used x=3

SELECT p.*,
COUNT(*) total_likes,
SUBSTRING_INDEX(
GROUP_CONCAT( CONCAT(u.firstname,'  ',u.lastname)  ORDER BY some_column DESC SEPARATOR '||'),
'||',3) x_no_of_likers
FROM product p
LEFT JOIN junction_table jt ON(p.id=jt.product_id)
INNER JOIN users u  ON(u.id=jt.user_id)
GROUP BY p.id

Fiddle

Now at your application level you just have to loop through the products and split the x_no_of_likers by separator you the likers per product

foreach($prods as $row):
$likers=explode('||',$row['x_no_of_likers']);
$total_likes= $row['total_likes'];
    foreach($likers as $user):
    ....
    endforeach;
endforeach;

Note there is a default 1024 character limit set on GROUP_CONCAT() but you can also increase it by following the GROUP_CONCAT() manual

Edit from comments This is another way how to get n results per group, from this you can get all the fields from your user table i have used some variables to get the rank for product group ,used subquery for junction_table to get the rank and in outer select i have filtered records with this rank using HAVING jt.user_rank <=3 so it will give three users records per product ,i have also used subquery for products (SELECT * FROM product LIMIT 30 ) so the first 30 groups will have 3 results for each,for below query limit cannot be used at the end so i have used in the subquery

SELECT p.id,p.title,u.firstname,u.lastname,u.thumbnail,jt.user_rank
FROM 
(SELECT * FROM `product` LIMIT 30 ) p
LEFT JOIN 
 (  SELECT j.*,
   @current_rank:= CASE WHEN @current_rank = product_id THEN  @user_rank:=@user_rank +1 ELSE @user_rank:=1 END  user_rank,
   @current_rank:=product_id
    FROM `junction_table` j ,
    (SELECT @user_rank:=0,@current_rank:=0) r   
    ORDER BY product_id 
  ) jt ON(jt.product_id = p.id)
  LEFT JOIN `users` u ON (jt.`user_id` = u.`id`) 
  HAVING jt.user_rank <=3 
  ORDER BY p.id

Fiddle n results per group

like image 111
M Khalid Junaid Avatar answered Feb 04 '26 20:02

M Khalid Junaid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!