SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    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:
    PHP Code:
    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>'
      }

    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    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.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    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).

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    *very confused*

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

    PHP Code:
    var_dump($row_rs_details); 
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    }
    Last edited by ScallioXTX; May 27, 2011 at 17:08. Reason: layout

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Looks like there are spaces in the filenames. Does this work

    PHP Code:
    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>'
      }

    ?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 ) 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

  10. #10
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    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!
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •