Jump to content

Running Two "foreach" loops?


MatthewSchenker

Recommended Posts

Greetings,

My first post here.  I'm learning PHP as rapidly as I can, but I still have some basic questions.

 

OK, I'm trying to run the Gallerific gallery script on my site.  The way Gallerific works, it looks at an unordered list to populate the gallery.  In each list item, it grabs a main image and a thumbnail image.

 

I have folders that are created dynamically as a user uploads photos.  There is a main folder and a "thumbs" sub-folder.  That's all taken care of before the script comes into play

 

I need to loop through both the main images and the thumbnails at the same time, and call each one in the list items.

 

I know this code must be wrong, but I'll post it as a starting place.  Can someone help me figure out how to do this correctly?  Maybe I should not even be using foreach?

 

<div id="thumbs" class="navigation">
                    <ul class="thumbs noscript">
<?php foreach($cck->get( 'property_listing_images' )->value as $large){
    foreach($cck->getValue( 'property_listing_images' )->value as $thumb1){ ?>
    <li>
    <a class="thumb" href="images/<?php echo $large->value; ?>" title="Title X">
    <img src="images/<?php echo $thumb1->value; ?>" alt="Title X" />
    </a>
    </li>
</ul>
<?php } ?>
<?php } ?>
</div>

 

Thank you ahead of time for your help!

 

Matthew

Link to comment
Share on other sites

I can't be sure this is the only problem without watching your code run, but you start the list before the loop and end it within the loop.

 

This is hard to see as you displayed the code, but becomes quite obvious if you indent it. Here is how it looks when indented according to my own habits. (Well, almost according to my own habits; improperly nested code can't be indented properly, which is part of the point here.)

 

   <div id="thumbs" class="navigation">
      <ul class="thumbs noscript">
<?php 
         foreach( $cck->get( 'property_listing_images' )->value as $large ) {
           foreach($cck->getValue( 'property_listing_images' )->value as $thumb1 ) {
?>
              <li>
                 <a class="thumb" href="images/<?php echo $large->value; ?>" title="Title X">
                    <img src="images/<?php echo $thumb1->value; ?>" alt="Title X" />
                </a>
              </li>
            </ul>
<?php 
         }
      }
?>
   </div>

Link to comment
Share on other sites

jhsachs,

Thanks for the quick help on this!  As you can see, I'm new with PHP *.

 

In doing a search on this, one problem has been that I don't know the technical description of what I'm trying to do.  Is it "simultaneous foreach loops"?  Also, I wasn't even sure that "foreach" was the right approach at all.

 

I'll definitely try the fix you suggested.

 

Thank you again,

Matthew

 

* Also, I'm still using Joomla (switching to a framework -- a whole other story)...

Link to comment
Share on other sites

"Nested loops" is the description I'm accustomed to.

 

A foreach loop seems like a reasonable solution to me. In most cases it's the only clear and reliable way to deal with iteration over an associative array, and it's often useful with indexed arrays too.

 

It looks to me like you have a good grasp of what you want to do and how to do it, and all you need is experience.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.