V
V
Viktor Volkov2018-04-04 21:24:55
Web development
Viktor Volkov, 2018-04-04 21:24:55

How to organize likes?

there is a page, depending on the selection in the controller (through the model), an array of elements (articles) is formed on it, which have buttons like and dislike. I decided to store all the likes in one table (user_id, article_id, like/dis), it will be easier with the statistics later (it seems to be done like this). When implementing, I encountered the following: I (as it seems to me) accessing the database too many times:
1. I get the array itself;
2. I check if the like has already been set (for each article !!!)
3. to display the total number of likes for the article again request.
4. well, if you liked the request too.
3-4 p. I will remake on AJAX, it will be simpler, but 2 p. Is it normal that in a selection of articles I have to check each with a separate request .... can anyone advise what? and in general about likes

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2018-04-04
@VGVolkov

do you use frameworks? they implement Model Relations very well.
And so:

1. I get the array itself;
but after all, left join or a subquery can hook the like / dislike statuses of a tech user
2. I check if the like has already been put (for each article !!!)
what prevents collecting all article IDs on the page and asking all the likes/dislikes of a current user with one request?
3. to display the total number of likes for the article again request.
again, you can either collect the entire number for current articles or left join or a subquery with a separate query
4. well, if you liked the request too.
this is an action, a record, it’s not a sin to donate resources

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question