Give advice. How can I add an unlimited number of photos with limited server resources? Database versions and directories are also considered. Something like this: there is an ad site - I can advertise and upload an unlimited number of photos there. What and how to store them? How more rational?
3 answers
Stand in response, it will be faster)
Tables:
records: id, name, text, ... images: id, rid, path, ...
For php + Mysql:
$record_id = 15; $img_id = 20; $page = 2; $path = '/images/img20.jpg'; // ΠΏΠΎΠ»ΡΡΠΈΡΡ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ ΠΊ ΠΎΠ±ΡΠ²Π»Π΅Π½ΠΈΡ: mysql_query('SELECT * FROM images WHERE rid='.$record_id.' LIMIT '.(15*($page-1)).', 15;'); // Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΠΊΠ°ΡΡΠΈΠ½ΠΊΡ ΠΊ ΠΎΠ±ΡΠ²Π»Π΅Π½ΠΈΡ: mysql_query('INSERT INTO images (rid, path) VALUES ('.$record_id.', \''.mysql_real_escape_string($path).'\');'); // Π΄ΠΎΠ±Π°Π²ΠΈΡΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΊΠ°ΡΡΠΈΠ½ΠΎΠΊ: mysql_query( 'INSERT INTO images (rid, path) VALUES ' . '('.$record_id.', \''.mysql_real_escape_string($paths[0]).'\'), ' . '('.$record_id.', \''.mysql_real_escape_string($paths[1]).'\'), ' . '('.$record_id.', \''.mysql_real_escape_string($paths[2]).'\') ' . ';' );
Yes, xref - cross references to the common people. The data from the two tables are linked by the third one, containing the ID from both tables.
This is called a many to many connection. Implemented by the third table.
The structure is approximately as follows:
announcements: id | title | ad_text | .... images: id | .... announcement_images: announcement_id | image_id
When adding an announcement, an entry is created in announcements. We also add 2 pictures, as a result we have 3 id, 1 id of the ad itself and 2 id pictures. Suppose the ad id is 1, and the id of the pictures is 1 and 2.
As a result, we need to insert 2 more entries in the announcement_images
1 | 1 1 | 2
UPD:
I'm talking about creating a third table for linking pictures with ads. With this scheme, you can upload an unlimited number of images to the ad. Why do you need to display 1000 pictures at once? Set a limit, for example in 10 pictures and a pager for pagination of pictures.
- But there is still a limit on the number. Or do you mean to create a separate table specifically for the pictures? Which in turn is related to the ad itself? Well, if I want to add, for example, 1000 pictures and at the same time they will be watched by 1000 users? Do not you think that the server will be hard? - new_russian_man
- Updated the answer. - Alex Kapustin
- oneThose. Do you want to load the Tu-154 onto the upper trunk of the frets of the priors and ask how to organize it?) 1000 pictures at the same time no one will watch - this will load the page of 10 minutes to wait. But if you care about it weakly - you need some dedicated server + dedicated (1-10) gigabit channel. Normal people take big hard and page navigation for pictures. By the way, the third table is optional - just the table images is done
[id|announcement_id|path]
. An XRef is done if the picture can relate to several records, and to the records - a few pictures, this is not the case. - Sh4dow 1:08 pm - Did not quite understand. But I will write, as I understand it. Create 3 tables Table - Ad ID | Techt | id_kartinkok Table - katrinki ID | picture | id_declarations cal table id | id ads | id pictures So? Or something I did wrong? - new_russian_man
- All wrong! ) You have mixed 2 ways. If you do not need to use the same image in different ads (and most likely do not need it), then use the method suggested by @ Sh4dow. This method involves 2 tables. - Alex Kapustin 1:49 pm
Iβll add then the 3rd version with one table in the database))
records: id, name, text, ...
On the server for each ad we create a folder with the name id from the table, and when displaying pictures, we look for a folder with the desired name and in a loop display all the pictures in it. If you need to delete the ad - delete the folder and all)
- and how to be with clustering? - Alex Kapustin
- Well, actually you / img / will be the second table, / img /% ID% / - primary index. A file system scan takes longer than a query to the database. In general, FS to touch for structuring is evil) - Sh4dow
- My version did not claim to "how to do", just the alternative already stated) [Sh4dow] [1] indicated the most appropriate, the 3rd table is superfluous. [1]: hashcode.ru/users/465/sh4dow - ilovetrancekrsk