Question

This topic maybe sound familiar but im not quite pleased with any of the Stackoverflow questions that are asked. See I´m creating a website with a small amount of members, say somewhere around 50. And I have created my registration form and my login form. But now I wan´t to assign a avatar to each new registered member. When i new member successfully register, a random avatar saved on the server will be assigned to them. The user will carry this avatar with them at every visit. So to clear my question:

How can I assign a random picture to each new user and save the picture to their user_id?

I'm currently using a MySQL and coding in PHP. I have a "Select-random-picture" function, but not sure how to store a picture with a user. I'm happy for any kind of advice.

EDIT

After some considerations, I have been able to state a clearer goal. I'm having images stored in a file system that i want to assign randomly to new members, AND when the image has been assign, it should be "taken". So if a new user register he will get a random image without the possibility to getting a already taken image.

I have only succeded in creating a generator that uploads random images, but not assuring that the same picture won't come again:

                    $avatarDir = 'members/images/avatars/';
                    $pics= glob($avatarDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
                    $randomPics = $pics[array_rand($pics)]; 
                    $query_new_user_picture = $this->connection->query("INSERT INTO my_images (image) VALUES('$randomPics')");
                    $results = mysql_query($query_new_user_picture);
Était-ce utile?

La solution

  1. Have a field in the database where you save the name/id/file path/whatever of the picture.
  2. Pick a random picture when the user registers (array_rand, shuffle, whatever).
  3. Save the randomly chosen picture in the field in the database.

Was that so hard?


$user = /* get user data from database */;

/**
 * assuming $user looks something like this:
 * 
 * array('name' => 'John Doe', 'email' => 'john@example.com',
 *       'profile_picture' => 'monster.jpeg');
 */

printf('<img src="img/profile_pictures/%s">', $user['profile_picture']);
// output: <img src="img/profile_pictures/monster.jpeg">

Now just make sure that image is accessible at that URL.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top