mysql - Get unique image for every user when call default image from query -


sorry, long title. have list registered users, profile pic, without. problem if user 1 logged in. in user list, has same profile image user 1.

but if remove default caller query, every user gets own picture, default.jpg image doesn't work. remove query , use if statement on page, want avoid that.

            function fetch_users($uid){             $query = $this->link->query( "select user.id, user.username, user.email,              userdetails.profile_img,userdetails.firstname,             userdetails.lastname,userdetails.location,following.follow_id             user             left join   userdetails on user.id = userdetails.user_id             left join   following on user.id = following.follow_id                   user.id != '{$uid}' ");             $users = array();             while(($row = $query->fetch(pdo::fetch_assoc)) !== false) {     #the row mess things#----->$row['profile_img'] = file_exists("img/{$uid}.jpg") ? "img/{$uid}.jpg" :  "img/default.jpg" ;             $users[] = $row;         }                return $users;         } 

you mixing stuff lot. in query fetch file name table, in comment row use uid construct name. former work default image, use

select …   ifnull(userdetails.profile_img, "img/default.jpg") profile_img, … 

this replace null values in table path of default image.

if wanted construct image paths user ids, should use id result row, not 1 of current user:

while(($row = $query->fetch(pdo::fetch_assoc)) !== false) {    $imgpath = "img/" . $row['id'] . ".jpg";   if (!file_exists($imgpath)) $imgpath = "img/default.jpg";   $row['profile_img'] = $imgpath;   $users[] = $row; } 

this create image path based on user id, , fall default if no such file exists.

you combine them both:

select …   ifnull(userdetails.profile_img,          concat("img/", user.id, ".jpg") profile_img, … 
while(($row = $query->fetch(pdo::fetch_assoc)) !== false) {    if (!file_exists($row['profile_img']))     $row['profile_img'] = "img/default.jpg";   $users[] = $row; } 

this use stored image path set, path constructed uid otherwise. use file named in fashion, default if no such file exist.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -