I have a txt file that has in it a painting id. This painting id is the same as the name of the image. For example in the txt file the painting id is 00000 and the name of the images in the path I wrote in my echo statement is 00000.jpg
.
What I'm trying to do is to display all the images using this for
loop and echo
statement. But for some reason the images are not displaying. Whats displaying instead is thats what is being displayed instead of the images. Can anyone help me solve this problem please?
<?php
$paintings = file('paintings.txt') or die('ERROR: Cannot find file');
$delimiter = '~';
foreach ($paintings as $painting){
$paintingFields = explode($delimiter, $painting);
$painting_id = $paintingFields[0];
}
for($i=0;$i<count($paintings); $i ){
echo "<img src='resources/paintings/square-medium/%s.jpg' . $painting_id></br>";
}
?>
CodePudding user response:
You're using echo
wrong. If you view the source of your generated page you'll see the literal '%s' in the code.
To use '%s' as a format string, use sprintf()
:
echo sprintf("<img src='resources/paintings/square-medium/$s.jpg'></br>", $painting_id);
Or:
echo "<img src='resources/paintings/square-medium/" . $painting_id . ".jpg'></br>";
Even better, just do the ouputting in PHP mode:
for($i=0;$i<count($paintings); $i ){
?>
<img src="resources/paintings/square-medium/<?= $painting_id ?>.jpg"><br>
<?php
}