0

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 :)

2
  • $check_pic = "$proppic"; What is this supposed to do? Commented May 27, 2018 at 21:16
  • Gets the image path from database, then if (file_exists($check_pic) Commented May 27, 2018 at 21:30

1 Answer 1

1

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\" />";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hello THANK YOU :D Sorted :D Well pleased
Did you also add the 2nd line of my code though, defining $num_rows?
No but when i put that in it sorted it as didn't notice it at first... Thank you Matthias :) Really appriciate that :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.