avatar with user information

Asked

Viewed 264 times

0

It is the following people, maybe this is very basic, but how do I associate the image of an image folder for example to the user id 1 in the bank? Will user 1 have image 01 of the img folder? Would you recommend creating the "folder path" with the name of the image in the database and create a relationship with the user table? Or have other techniques more useful and currently used for this?

1 answer

2


Create a column in the user table to store the avatar path according to its structure:

ID NOME  AVATAR
1  Asura img/avatares/123456.jog

Now whenever you have user registration you will have the image path to load:

<img src="<?php echo $usuario->avatar;?> />

I find this way interesting because you can separate the images by context, let’s assume you have the table produtos, you can create the directory img/produtos and when saving the product, you place the image path in the record or even in another table if the product has more than one image.

  • Just one question, I don’t know if you can, but do you know how I would make each user have more than one related image? And thank you very much!

  • @In this case you should create a table to store the paths. A table for example, UsuariosAvatares with the columns id, usuario_id and avatar. It is also possible, if you have a limit of two images, for example, to use a separator in the record: img/avatares/123.jpg;img/avatares/321.png so you’d only make one split() in the ; and would have a array with the paths of the images.

  • Caraka, perfect! I completely forgot about Split. And you said I’d like to create this "img/avatars/123456.jpg", this 123456 would be the id? Or it could be a random number?

  • @Asurakhan can be the name of the image or anything you find interesting. I like to put timestamp so I don’t run the risk of having images with duplicate names and end up overwriting one of them. That way it would be something like img/avatares/17072015150955.jpg

  • So when you upload the image to the folder, I also add the path to the database in a normal register with id and information?

  • @Exact Asurakhan =)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.