Reputation: 49
I have a while loop to get information and within the loop i have another loop in order to get the information for images which are in a different table relation to the main loop... Gathering and displaying information is fine, getting the images relating to information is working ok accept when there is no image path in the image table.. (ie: if no one has uploaded a picture) It's not an actual image in Mysql, the the path to the image...
For EG:
Id 1 = No image and shows no image pic
Id 2 = Image and Shows the image
Id 3 = No Image but shows image from Id 2 (Should show no image pic)
Id 4 = Image and Shows the image
Id 5 = Image and Shows the image
Id 6 = No Image but shows image from Id 2 (Should show no image pic)
Id 7 = No Image but shows image from Id 2 (Should show no image pic)
$sql_props = mysqli_query($db_conx, "SELECT `image` FROM `images` WHERE `id`='$id' LIMIT 0,1");
while($lex = mysqli_fetch_array($sql_props)){
$proppic = $lex["image"];
}
$check_pic = "$proppic";
if (file_exists($check_pic)) {
$pr_spic = "<img src=\"$proppic\" width=\"100px\" height=\"100px\" border=\"0\" />";
} else {
$pr_spic = "<img src=\"images/nopimg.png\" width=\"100px\" height=\"100px\" border=\"0\" />";
}
Thank you and hope someone can help with this please :)
Upvotes: 0
Views: 415
Reputation: 459
The problem is most likely that you define the variable $proppic
in one iteration of the while loop and then later on access it again, thinking that the variable should be unset because it appears to be out of scope.
I will try to give you a solution while modifying your code as little as possible.
$sql_props = mysqli_query($db_conx, "SELECT `image` FROM `images` WHERE `id`='$id' LIMIT 0,1");
$num_rows = $sql_props->num_rows;
while($lex = mysqli_fetch_array($sql_props)){
$proppic = $lex["image"];
}
$check_pic = "$proppic"; //bad style
if ((file_exists($check_pic)) && ($num_rows > 0)) {
$pr_spic = "<img src=\"$proppic\" width=\"100px\" height=\"100px\" border=\"0\" />";
} else {
$pr_spic = "<img src=\"images/nopimg.png\" width=\"100px\" height=\"100px\" border=\"0\" />";
}
Upvotes: 1