Jump to content

Reference Stored Image


unemployment

Recommended Posts

I am writting a php function that uses mysql to get user data - pretty common, right :)

 

Well, my issue is that I need to run a check in my file system.  Users profile pictures are stored in my image directory as .png's.  I need to have my function check that directory and if an image matches their id, then return their information.  I only want the user data if they have an image uploaded.

 

Here is my current function:

 

function fetch_users_login($limit)
{
$limit = $limit(int);

$sql = "SELECT
                `users`.`id`,
                `users`.`firstname`,
                `users`.`lastname`,
                `users`.`username`,
			`user_privacy`.`avatar`
            FROM `users`
		LEFT JOIN `user_privacy`
            ON `users`.`id` = `user_privacy`.`uid`
		WHERE `users`.`status` > 2
		AND `user_privacy`.`avatar` = 1 
		ORDER BY `users`.`id` DESC
		LIMIT 0, {$limit}";

$result = mysql_query($sql) or die(mysql_error());

$users = array();

$i = 0;

while(($row = mysql_fetch_assoc($result)) !== false)
{
	$users[$i] = array(
		'id' 				=> $row['id'],
		'firstname' 		=> $row['firstname'],
		'lastname' 		=> $row['lastname'],
	);

	$users[$i]['avatar']	= getUserAvatar($row['username']);

	$i++;
}

return $users;
}

Link to comment
Share on other sites

I would suggest using a table specifically for images. 

 

Table user_images:

imageid

userid

image_name

image_path

image_thumb

image_default (set it to 0 or 1, 1 = profile image, 0 = just an image for that user)

 

Then, when you look for users with images, then you can look up this table, and join the users table with the userid.

 

This should make it a lot simpler to handle the images of each user.

 

 

Link to comment
Share on other sites

I see you have a function called getUserAvatar($row['username'])

 

In that function I would just do a check if the image exists is their value, else display a default user image that you create

To me this reminds them every day that they never uploaded an image.

something like...

$user_image_location = "/images/$username.png";
if (file_exists($user_image_location)) {
    $user_avatar = $user_image_location;
} else {
    $user_avatar = "/images/default-user-avatar.png";
}

 

The way I do this all myself is that i just pull the users table and all info.

I have a user_avatar field, at user creation I insert my default-user-avatar.png as their image

If they decide to upload their own, their new image name is updated in the database

 

or can leave the field empty and if not empty and file_exists....display their image

Link to comment
Share on other sites

how are you currently naming/identifying a picture as belonging to a particular user?

 

Just a little more background on this...

 

The goal of the function is to get the most recent users with profile pictures uploaded. 

 

I am naming the images by id... so uid of 1 will have picture 1.png and so forth. 

 

 

Link to comment
Share on other sites

I see you have a function called getUserAvatar($row['username'])

 

In that function I would just do a check if the image exists is their value, else display a default user image that you create

To me this reminds them every day that they never uploaded an image.

something like...

$user_image_location = "/images/$username.png";
if (file_exists($user_image_location)) {
    $user_avatar = $user_image_location;
} else {
    $user_avatar = "/images/default-user-avatar.png";
}

 

The way I do this all myself is that i just pull the users table and all info.

I have a user_avatar field, at user creation I insert my default-user-avatar.png as their image

If they decide to upload their own, their new image name is updated in the database

 

or can leave the field empty and if not empty and file_exists....display their image

 

the getUserAvatar function that I have just get either the users avi or the default if no image exists.  I'm not sure if you suggestion fits my goal.

Link to comment
Share on other sites

would unsetting that user if no file exists work?

 

function fetch_users_login($limit)
{
$limit = $limit(int);

$sql = "SELECT
                `users`.`id`,
                `users`.`firstname`,
                `users`.`lastname`,
                `users`.`username`,
			`user_privacy`.`avatar`
            FROM `users`
		LEFT JOIN `user_privacy`
            ON `users`.`id` = `user_privacy`.`uid`
		WHERE `users`.`status` > 2
		AND `user_privacy`.`avatar` = 1 
		ORDER BY `users`.`id` DESC
		LIMIT 0, {$limit}";

$result = mysql_query($sql) or die(mysql_error());

$users = array();

$i = 0;

while(($row = mysql_fetch_assoc($result)) !== false)
{
	$users[$i] = array(
		'id' 				=> $row['id'],
		'firstname' 		=> $row['firstname'],
		'lastname' 		=> $row['lastname'],
	);

	$users[$i]['avatar']	= getUserAvatar($row['username']);

	 if (!file_exists($users[$i]['avatar'])) {
                 unset($users[$i]);
                 }


	$i++;
}

return $users;
}

Link to comment
Share on other sites

would unsetting that user if no file exists work?

 

function fetch_users_login($limit)
{
$limit = $limit(int);

$sql = "SELECT
                `users`.`id`,
                `users`.`firstname`,
                `users`.`lastname`,
                `users`.`username`,
			`user_privacy`.`avatar`
            FROM `users`
		LEFT JOIN `user_privacy`
            ON `users`.`id` = `user_privacy`.`uid`
		WHERE `users`.`status` > 2
		AND `user_privacy`.`avatar` = 1 
		ORDER BY `users`.`id` DESC
		LIMIT 0, {$limit}";

$result = mysql_query($sql) or die(mysql_error());

$users = array();

$i = 0;

while(($row = mysql_fetch_assoc($result)) !== false)
{
	$users[$i] = array(
		'id' 				=> $row['id'],
		'firstname' 		=> $row['firstname'],
		'lastname' 		=> $row['lastname'],
	);

	$users[$i]['avatar']	= getUserAvatar($row['username']);

	 if (!file_exists($users[$i]['avatar'])) {
                 unset($users[$i]);
                 }


	$i++;
}

return $users;
}

 

That works, but it doesn't work with a limit of 5.  I want to limit the SQL when 5 images have been found.  Any way to do this?

Link to comment
Share on other sites

What will happen if the user has 5 images? All of those images will be named 1.jpg? 1.jpg, 1.jpg and so on...

 

I still suggest my approach

I would suggest using a table specifically for images. 

 

Table user_images:

imageid

userid

image_name

image_path

image_thumb

image_default (set it to 0 or 1, 1 = profile image, 0 = just an image for that user)

 

Then, when you look for users with images, then you can look up this table, and join the users table with the userid.

 

This should make it a lot simpler to handle the images of each user.

 

 

Link to comment
Share on other sites

What will happen if the user has 5 images? All of those images will be named 1.jpg? 1.jpg, 1.jpg and so on...

 

I still suggest my approach

I would suggest using a table specifically for images. 

 

Table user_images:

imageid

userid

image_name

image_path

image_thumb

image_default (set it to 0 or 1, 1 = profile image, 0 = just an image for that user)

 

Then, when you look for users with images, then you can look up this table, and join the users table with the userid.

 

This should make it a lot simpler to handle the images of each user.

 

 

 

Good point, but my system isn't that dynamic.  The user can currently only upload one picture.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.