Reputation: 27
I want to read 5 attributes of the database. The 5 attributes have the names post_image_1
, post_image_2
, post_image_3
, post_image_4
and post_image_5
. Now I want to show the 5 images on my page, with a for loop.
Here is the loop:
for($i = 1; $i <= 5; $i++){
echo "<img src='image/$row[post_image_$i].png' height='250px' width='250px'>";
}
Now I get an error:
Parse error: syntax error, unexpected '$i' (T_VARIABLE), expecting ']' in
I hope that's enough info to help me. :P
Upvotes: 0
Views: 57
Reputation: 809
Try this, using string concatenation, it gets at the right field in the array. I'm assuming $row is declared already and contains the keys 'post_image_1', 'post_image_2'...
for($i = 1; $i <= 5; $i++){
echo "<img src='image/" . $row['post_image_' . $i] . ".png' height='250px' width='250px'>";
}
Upvotes: 2
Reputation: 3158
Since you're using arrays, do this:
for($i = 1; $i <= 5; $i++){
echo '<img src="image/'.$row['post_image_'.$i].'png" height="250px" width="250px">';
}
Upvotes: 4