L
L
legolas44442014-10-27 20:06:17
MySQL
legolas4444, 2014-10-27 20:06:17

Realization of the wall like Vkontakte?

Hello! Can anyone tell me how to implement the output of posts and the last 3 comments to it? Well, how did they do it in VK. You make a request and the output is something like

array (
      array ("title" => "Статья","author" => "Автор","comments" => "массив с комментариями")
)

Tell me which way to dig.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Entelis, 2014-10-27
@DmitriyEntelis

What's the problem?
2 requests:

select * from posts where post_id = 123;
select * from comments where post_id = 123;

In php you collect in a single
Understood the question. The question is interesting. The task is often encountered, there are several options:
1. Think about whether you really need to return just such a data structure.
It may be easier to make additional requests from the interface. And the list of posts will load faster, and the implementation is simpler.
2. If you want to give just such a structure, you can use, for example, a cache.
In the table of posts, we start a field and put in it a json line with the last 3 comments.
When someone adds/deletes a comment, we update the field.

A
Anton Peat, 2014-10-27
@id_aspin

Q = Number of comments to the id of this post (for example)
Under the post, you display comments with the id of the post and the id of the comment Q, Q-1 and Q-2

L
legolas4444, 2014-10-27
@legolas4444

No-no) You do not understand) You say if one post. And in my cycle these records are displayed. That is, you need to get the record from the database in such a way that I could make a cycle and make another cycle inside the cycle that displays comments. If you make requests in a cycle, then I think this is not quite good.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question