Problems with for loop

I want to display images associated with a record. The number of images varies, up to a max of 8. I have the correct php (shown here)

 if(!empty($row_rs_details['image1'])){ 
  echo "<img src=\\"http://www.sitepoint.com/forums/images/",$row_rs_details['image1'],"\\"><br>"; }
  if(!empty($row_rs_details['image2'])){ 
  echo "<img src=\\"http://www.sitepoint.com/forums/images/",$row_rs_details['image2'],"\\"><br>"; }
  if(!empty($row_rs_details['image3'])){ 
  echo "<img src=\\"http://www.sitepoint.com/forums/images/",$row_rs_details['image3'],"\\"><br>"; }
..../ etc. for 8 images

This works great. But all that repetition is perfect fodder for a for loop, so I tried one, thusly

  for ($i = 1; $i <= 8; $i++) {
  $image = "image".$i;
  if(!empty($row_rs_details['$image'])){ 
  echo "<img src=\\"http://www.sitepoint.com/forums/images/",$row_rs_details['$image'],"\\"><br>"; 
  }
}

Which doesn’t work so great. When I echo $image alone it shows the correct value (i.e. image1, image2, etc.), therefore I must be missing a quote or double quote or escape somewhere. I need another pair of eyes to see what I’m missing.

Thanks :slight_smile:

The problem is the single quotes in $row_rs_details['$image'] since variables in single quotes aren’t parsed. So instead of asking for ‘image1’, ‘image2’, etc, you’re litterally asking for the key ‘$image’, which doesn’t exist, for obvious reasons.

Also, if you use single quotes for the string you don’t have to escape double quotes within that string, which makes it look a lot nicer IMO. and it’s marginally faster as well, in case anyone cares

This should work:


for ($i = 1; $i <= 8; $i++) {
  $image = 'image'.$i;
  if(!empty($row_rs_details[$image])) { 
    echo '<img src="http://www.sitepoint.com/forums/images/', $row_rs_details[$image], '"><br>'; 
  }
} 

Yeah I thought of that, and tried it again just now copying and pasting your code but it didn’t work.

And thanks for the tip about the single quotes :slight_smile:

It didn’t work? Are you sure there is data in the rows?
The snippet I posted is the exact same as the first snippet in your post; they should behave exactly the same.

Yes I’m sure there’s data in the rows because the images display correctly if I use the long code I posted (without the loop).

very confused

Could you put this above the code and post what the output is?


var_dump($row_rs_details);

Result of var_dump:

array(13) {
<snip because not relevant />
[“image1”]=> string(24) “norwood bathroom.jpg”
[“image2”]=> string(19) “norwood bedroom.jpg”
[“image3”]=> string(20) “norwood bedroom1.jpg”
[“image4”]=> string(19) “norwood kitchen.jpg”
[“image5”]=> string(20) “norwood kitchen1.jpg”
[“image6”]=> string(18) “norwood livingroom”
[“image7”]=> NULL
[“image8”]=> NULL
}

Looks like there are spaces in the filenames. Does this work


for ($i = 1; $i <= 8; $i++) {
  $image = 'image'.$i;
  if(!empty($row_rs_details[$image])) { 
    echo '<img src="http://www.sitepoint.com/forums/images/', urlencode($row_rs_details[$image]), '"><br>'; 
  }
} 

?

That worked, but mainly because I noticed that the image source was set to this forum for some reason (Don’t know how that got in there :blush:) It isn’t in the original code, but when I copied and pasted yours it had the wrong url so of course it didn’t work. When I set the source to simply “images/[filename]” , presto!

Thanks a lot Scallio :slight_smile:

Ah yes, the forum software has a tendency to screw up relative URLs lately. It’s on the TODO list to be fixed.

Glad you got it working! :slight_smile: