I have a products table which contains information about products.
Each products can have unlimited pictures. These pictures are stored in a folder named after their product's id, and that's fine as it is, I don't need database to store file names.
But each of these pictures have text captions.
My question is, what is the best solution for storing these captions data?
Should I create a table called products_captions, with the fields
product_id, image_name, caption_text
Or should I create a captions_array field in the product table, containing a serialized array of strings, with the array key corresponding to the file name?
Is there any standard for this???
As per your requirement you can create another table for storing the images name with their captions and product id so when you want to get the caption related to product you can get it by passing product id to the table.
store your caption in another table and map the Image and caption
for example
field column
--------------------
id int
Image Blob
captionID int
field column
----------------------
captionID int
cation varchar(50)
Related
I have a PHP frontend with an image gallery. The data/metadata for the images is pulled from a sqlite database table.
Now I'd like the user to be able to tag the images, e.g. giving image1 the tags "boat" & "fish", and giving image2 some other or the same tags. How would I set up the database for this?
My idea is to create a new table where each row is a tag with an ID.
But how do I add these IDs into the image details table to connect both tables? Afaik I can't simply add a new column to the image table that contains the tag ids as an array, since sqlite doesn't support that.
How could I do this?
Yes, you would have to create a new table for tags.
In your case, best thing would be to create an intermediate table, (let's call it image_tag) which has an image_id and a tag_id column. Now if you want to let's say assign two tags to an image, you can just insert 2 records in the image_tag table containing that image's id and the ids of the tags.
This is called a many to many relationship as one image could have more than one tag and more than one image could have one tag.
Maybe this could give you a rough idea:
Image Table
id path
1 image.jpg
Tags Table
id name
1 fish
2 boat
image_tag Table
id image_id tag_id
1 1 1
1 1 2
For more info, please refer to this.
If you don't want to use child table then you can add tag in same row same table using serialize fields.
Like,
$returnValue = serialize(array('fish','boat'));
ID IMAGE TAG
1 Image.jpg a:2:{i:0;s:4:"fish";i:1;s:4:"boat";}
And you can simply read this $returnValue = unserialize($row['TAG']);
I've got some question(s)
How many tables should i create for my products ?
2???=products & products pics???
if yes then my mysql tables will be like that ?
1.products= p_id, p_name, p_descr, p_pieces, p_categ, p_subcateg(??) &
2.products_pics= pp_id, product_id, thumb, image, type (???)
Well im a little bit confused cause i think its not so necessary to use products_pics into the db anymore .
My product entries will be around 3.000 for the beginning . Can someone tell me what im going to write-do ??? Thank you very much !
You should make 3 tables
products
images
products_images
In table products you have id_prod (primary key), name_prod, and other.
In table images you have id_img (primary key), name_img or img_src.
In table products_images you link images with products, and structure should be like id (primary), id_prod, id_img.
This way you will not have a duplicate content in tables.
If you only have images / documents pertaining to products only, and you want to store them in your database, you can store the image as blob/longblob directly inside the products table as p_image for example.
But when you want to retrieve it you will get the binary data, so you should write it to a file or convert it to view it as image under browser.
And if you have multiple images for the same product, you should create another table and use p_id as foreign key.
However, it is not advisable to store images in the database, for more information please refer to : Storing Images in DB - Yea or Nay?
I have an idea where a user can upload a maximum of 5 images for his or her gallery and can select one of them as his or her profile pic. Is it possible to save the names of all 5 pics in a single column, say, image? Or do I have to make 5 separate columns for storing these file names?
As an example, I could create a column called images and store values in this manner:
{"129cc68678d1e67d18d49d93cec040ce":{"filename":"129cc68678d1e67d18d49d93cec040ce.jpg"},"97ac1e22ea3fd6f02362233d8147ecb5":{"filename":"97ac1e22ea3fd6f02362233d8147ecb5.jpg","primary":true},"eeb239b4edff7e1a09246d38977a5646":{"filename":"eeb239b4edff7e1a09246d38977a5646.jpg"},"55a3810c1b0eaee039c753c12dccbc38":{"filename":"55a3810c1b0eaee039c753c12dccbc38.jpg"}}
Primary:true implies that it's a featured image/ profile picture.
Both of your proposals --- 5 pieces of information in one column and 5 columns --- are bad ideas. What if you ever want to change the limit to 4, or 6, or 20?
Normalize your data instead. Create a table for the images and another for the user-image relationships. In that second table, set a flag for the profile picture.
What you will want to do instead, is create another table to hold your image filenames. Your galleries table would hold an ID and the other info you are storing for the gallery, then your images table would have three columns. One for its ID, one for the filename, and one to store the ID of the gallery it belongs to (called a foreign key).
I'm designing a site where comments can be added in reply to an initial post and each comment can have attached images (and likely will given the type of content). I'm wondering how I will grab the proper comment ID to insert it into a MySQL DB column in the image table.
So far the db has a table for the initial post with serial as the primary key, the db also has an image table with id as the primary and columns for various attributes of the image as well as a column for the serial of the post images belong to (serial comes from the serial of the item the post is about), I plan to also add a column to that table for the comment ID which will be filled with the ID of the comment they belong to if they don't just belong to the initial post. I'd like to add comments in their own table with ID, info, date and title.
What I'm unsure of is when inserting a comment with attached image, how do I grab the comment ID to insert into the image table comment ID column? Or is there a better way to approach the issue?
Thanks in advance for any advice.
Suppose a table for products in database. Now product has an "image" field where i will place the image name "image.jpg". Now i want to display more than one images of same product. image1.jpg, image2.jpg , image3.jpg.How can i add these records into that product table in single field and how to implement it in PHP to display those images. And the same scenario in terms of size of product i.e medium, large, extra large etc.
I will assume you have already weighed the pros and cons of storing a file name instead of storing a BLOB.
This sounds like a 1-to-many relationship. You will want a table that holds -- at a minimum -- the file name and a foreign key (FK) for the product table. Then you can have multiple images pointing to the same product FK. After that, in order to get the image for a single product, you would just do a query such as:
SELECT img.fileName
FROM image_table AS img
JOIN product_table prod ON prod.id = img.prod_id
WHERE prod.id = ID_THAT_YOU_WANT
I'm ignoring issues with SQL injection, but you shouldn't. Don't just append the ID to the query.