Reputation: 49
Could someone please show me where i'm going wrong here, i've been at it for hours and cannot seem to get this to work... Pictures showing if member has uploaded... When upload a pic it puts details in mysql DB (picture.jpg)
I'm having an issue trying to show a default pic if a picture hasn't been uploaded. I've tried various different ways including numrows but somethings not right i'm not getting the default picture for some reason.... Any help would be appriciated Thank you
$sql_com = mysqli_query($db_conx, "SELECT `photo` FROM `members` ORDER BY `members`.`registered` DESC LIMIT 7");
$hpmembers = '';
while($row = mysqli_fetch_array($sql_com)){
$photo = $row["photo"];
if (file_exists('photos/' . $photo . '')) {
$user_pic = '<img src="photos/' . $photo . '" width="94" height="94" border="0" style="padding:2px;border:#e6e6e6 solid 1px;margin:3px;" />';
}else{
$user_pic = '<img src="photos/avatar.gif" width="94" height="94" border="0" style="padding:2px;border:#e6e6e6 solid 1px;margin:3px;" />';
}
$hpmembers .= '' . $user_pic . '';
}
Also tried:
$sql_com = mysqli_query($db_conx, "SELECT `photo` FROM `members` ORDER BY `members`.`registered` DESC LIMIT 0, 14");
$i = 0;
$hpmembers = '<table border="0" cellpadding="5">';
$num_rows = $sql_com->num_rows;
while($row = mysqli_fetch_array($sql_com)){
$photo = $row["photo"];
if ((file_exists('photos/' . $photo . '')) && ($num_rows > 0)) {
$user_pic = '<img src="photos/' . $photo . '" width="50" height="50" border="0" />';
}else{
$user_pic = '<img src=\"photos/avatar.gif\" width="50px" height="50px" border="0" />';
}
if ($i % 7 == 0) {
$hpmembers .= '<tr><td>' . $user_pic . '</td>';
} else {
$hpmembers .= '<td>' . $user_pic . '</td>';
}
$i++;
}
$hpmembers .= '</tr></table>';
Upvotes: 0
Views: 32
Reputation: 1219
@Forbs answer is good, I'm just providing another approach.
I would check if your query returned any rows. If it did, then there is a photo, if it didn't then display the default pic
$sql_com = mysqli_query($db_conx, "SELECT `photo` FROM `members` ORDER BY `members`.`registered` DESC LIMIT 7");
if (!mysqli_num_rows($img_result)){ //If nothing is returned from the database, then display default image
$user_pic = '<img src=\"photos/avatar.gif\" width="50px" height="50px" border="0" />';
}
else{ //If there is a location returned, display the image with the image path
while($row = mysqli_fetch_array($sql_com)){
$photo = $row["photo"];
$user_pic = '<img src="photos/' . $photo . '" width="50" height="50" border="0" />';
}
I did it this way because I code with negative checking in mind. but this should also give the same result :D
Upvotes: 1
Reputation: 1276
If $photo
is blank then the directory photos/
is existing
Change
if (file_exists('photos/' . $photo . '')) {
to
if (file_exists('photos/' . $photo . '') && $photo != '') {
Upvotes: 2