Answer the question
In order to leave comments, you need to log in
How to organize file attachments to posts?
Hello!
I have a social network site, on the site users can add posts (records) to their page.
I also want to be able to attach files from the site section (for example, photos, audio recordings, videos), let's say everything is exactly like Vkontakte is now, but I can't figure out the structure of the database table for posts, how it should be and how to display it all later.
approximate structure of a table with posts
id - post id
self_id - on which page
author_id is posted - who wrote the post
text - post text
I thought to add the attach_audio field to this table for attached audio recordings with their IDs (after all, you can add not one but several) ( e.g. 10,30,55)
but how then can you make a single request to display the recording and attached audio recordings.
Thank you all for your feedback!
Is it possible to somehow make a request so that the output is
array (
'id' => 1,
'self_id' => 1 ,
'user_id' => 1 ,
'text' => 'text post',
'attach_audio' => array (
0 => array (
'audio_id' => 10 ,
'title' => 'track title'
)
)
)
say something like this
SELECT post.*, (SELECT audio.* FROM audio WHERE audio IN (post. attach_audio)) as attach_audio
FROM post
?????????
Answer the question
In order to leave comments, you need to log in
1. You can store all attachments for a message in a separate field in json format, for example. Display attachment via javascript, without extra requests, just generate a link on the fly. For example, by hash from id + '.mp3' and get a link to the attachment in mp3. The attachment type can also be stored in json in a specific property.
2. You can make several additional requests and get ready-made links on the server side.
3. You can make a separate table for all attachments and get them when displaying a message by message_id.
+ the latest messages can (and probably should) be cached, so, offhand, any method will do, I still don't understand the critical places.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question