unemployment Posted November 30, 2011 Share Posted November 30, 2011 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; } Quote Link to comment Share on other sites More sharing options...
jotorres1 Posted November 30, 2011 Share Posted November 30, 2011 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. Quote Link to comment Share on other sites More sharing options...
unemployment Posted November 30, 2011 Author Share Posted November 30, 2011 I was thinking about doing that, but I just didn't want to do the migration. It does seem like it's my easier bet though. Quote Link to comment Share on other sites More sharing options...
jotorres1 Posted November 30, 2011 Share Posted November 30, 2011 Trust me, you will avoid unnecessary headaches in the future. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted November 30, 2011 Share Posted November 30, 2011 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 Quote Link to comment Share on other sites More sharing options...
litebearer Posted November 30, 2011 Share Posted November 30, 2011 how are you currently naming/identifying a picture as belonging to a particular user? Quote Link to comment Share on other sites More sharing options...
unemployment Posted November 30, 2011 Author Share Posted November 30, 2011 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. Quote Link to comment Share on other sites More sharing options...
unemployment Posted November 30, 2011 Author Share Posted November 30, 2011 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. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted November 30, 2011 Share Posted November 30, 2011 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; } Quote Link to comment Share on other sites More sharing options...
unemployment Posted November 30, 2011 Author Share Posted November 30, 2011 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? Quote Link to comment Share on other sites More sharing options...
jotorres1 Posted November 30, 2011 Share Posted November 30, 2011 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. Quote Link to comment Share on other sites More sharing options...
unemployment Posted November 30, 2011 Author Share Posted November 30, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.