I am having problems displaying images which have been queried from the database. The array from the query result is looped through and the image name is extracted, then passed to a variable which also contains the image path. However when the variable is included in the output, a torn image icon is displayed (no image).
Code:
public function loadImages($item) {
$type = $item;
$conn = $this->create_connection('read');
$sql = "SELECT * FROM `items` WHERE catagory='$type'";
$result = $conn->query($sql)or die(mysql_error());
while($row = mysqli_fetch_array($result)) {
$path = '../images/thumbs/'.$row['image_path'];
//var_dump($path);
echo '<li><img src="../images/thumbs/'.$path.'"/></li>';
}
When I var_dump $path, it displays the correct file path: string(28) "../images/thumbs/image1.jpeg", with a further seven items, so I am not losing data. I can confirm the path is correct, when I echo the full path and image name, the image displays correctly.
echo '<li><img src="../images/thumbs/image1.jpg"/></li>';
Also I have tried removing the $path variable and applying $row directly:
echo '<li><img src="../images/thumbs/'.$row['image_path'].'"/></li>';
I thought the problem was with joining the two strings, so I tried replacing the image variable with $full_path, which also did not work:
$full_path = $path . $row['image_path'];
$row is correct because when I do a var-dump for $row['image_path'] I get the following:
string(11) "image1.jpeg" string(11) "image2.jpeg" string(11) .....
I previously had the full image path held within the database, when the full image path was used, the images displayed correctly. I later decided only to store the image name in the database, as the path could change in the future, so the design change created this problem.
$path already contains “../images/thumbs/”, but you add it again when echoing the line.