Jump to content
Fran

[Solved] 2 arrays inside 1 foreach

Recommended Posts

Hi, I'm using this piece of code to retrieve the names of two arrays of images and it works really well. But my question is how can I use only one foreach so i can use both arrays inside of it? Thanks very much.

$precioschico = $page->get("planos");
$preciosgrande = $page->get("mapas");

foreach( $preciosgrande as $preciogrande ) {
		echo $preciogrande . '<br/>';
		}

foreach( $precioschico as $preciochico ) {
		echo $preciochico . '<br/>';
		}

 

Share this post


Link to post
Share on other sites

Sorry, was on mobile so couldn't add a code sample. 

$precioschico = $page->get("planos");
$preciosgrande = $page->get("mapas");
$precios = $precioschico->makeCopy(); // So we don't change the variable itself on the next line
$precios->import($preciosgrande);

foreach( $precios as $precio ) {
		echo $precio . '<br/>';
}

 

  • Like 3

Share this post


Link to post
Share on other sites

Another approach using WireArray::and() which implicitly creates a copy of the Wire(or Page)Array:

$precioschico = $page->get("planos");
$preciosgrande = $page->get("mapas");
$precios = $precioschico->and($preciosgrande);
// Or all in one step: $precios = $page->get("planos")->and($page->get("mapas"));

foreach( $precios as $precio ) {
		echo $precio . '<br/>';
}

 

  • Like 4

Share this post


Link to post
Share on other sites

Thanks LostKobrakai. You're being really helpful. This is the first time I have both arrays on the same foreach. I'm intending to use it so I can click on an image from one array and it opens its corresponding bigger(and slightly different) from the other one. I really don't know if I'm going into the right direction with this.

I'd like to achieve something like this on the foreach:

echo "<div class='col-md-6 col-lg-4'>";
echo "<a href='{$preciosgrandeimg->url}' class='thumbnail' rel='prettyPhoto[galeriaplanos2]'>
<img class='img-responsive' src='{$precioschicoimg->url}' />
</a>";
echo "</div>";

Maybe I'm a little more clear with this.

Thanks very much for your help.

Share this post


Link to post
Share on other sites
5 hours ago, BitPoet said:

Another approach using WireArray::and() which implicitly creates a copy of the Wire(or Page)Array:


$precioschico = $page->get("planos");
$preciosgrande = $page->get("mapas");
$precios = $precioschico->and($preciosgrande);
// Or all in one step: $precios = $page->get("planos")->and($page->get("mapas"));

foreach( $precios as $precio ) {
		echo $precio . '<br/>';
}

 

Thanks Bit Poet for your help... maybe I'm not trying to combine them into one array but rather work with both inside the foreach statement. Please take a look at my response to LostKobrakai above.

Thanks very much again.

Share this post


Link to post
Share on other sites

I see, that's of course different, and - since there's no direct relationship between two image fields - doable but dangerous in case one of the images is missing, and keeping then in sync isn't easy once the list gets long.

$precioschico = $page->get("planos");
$preciosgrande = $page->get("mapas");

for($i = 0; $i < $precioschico->count(); $i++) {
  $chicoimg = $precioschico->eq($i);
  $grandeimg = $preciosgrande->eq($i);
  echo "
    <div class='col-md-6 col-lg-4'>
      <a href='{$grandeimg->url}' class='thumbnail' rel='prettyPhoto[galeriaplanos2]'>
        <img class='img-responsive' src='{$chicoimg->url}' />
      </a>
    </div>
  ";
}

To make the code a little simpler and avoid errors by images not matching up, you might consider creating a repeater field with two image fields (one named "grande" and one "chico", each set to "Single item"). Then you could run a simple loop over your repeater field.

foreach($page->repeaterimages as $img) {
  echo "
    <div class='col-md-6 col-lg-4'>
      <a href='{$img->grande->url}' class='thumbnail' rel='prettyPhoto[galeriaplanos2]'>
        <img class='img-responsive' src='{$img->chico->url}' />
      </a>
    </div>
  ";
}

If you set the column width for both image fields to 50%, you can show and edit them side by side in the repeater list on the page. The page editor would look like this:

TwoImageRepeater.png

  • Like 3

Share this post


Link to post
Share on other sites

Thanks BitPoet !!! You really save my life on this one !! I can't thank you enough... :rolleyes:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Atlasfreeman
      Hi!
      I want to make a small site, a one page site.
      And i have this idea about doing 2 to 3 diffrent template that i can load into the index / home page.

      I want to do this with an array so that i can keep creating more topics (with the template) id needed.
      <?php include('./head.inc'); // include header markup ?> <?php $children = $page->get('template=onecolmn|twocolumn, sort=sort'); foreach($children as $child) { include($child); } ?> <?php include('./foot.inc'); // include footer markup ?>  
      As you can see i have to template wish i want to control from the backend.
      I know include does not work this way, put what is my other option to make an array that loads the whole page on an other pages.
       
       
    • By louisstephens
      From my last post, I was given a good idea on how to count the repeater items, and it worked wonderfully. I got my code working well and the columns (based on the count) all work well as well. Now, I have a head scratcher on my hands. 
      <?php $buttonsIncluded = $page->special_custom_buttons->find('special_custom_buttons_include=1'); $buttonsIncludedCount = count($buttonsIncluded); $buttonsIncludedCountAdditional = $buttonsIncludedCount +1; echo $buttonsIncludedCount; ?> <div class="row"> <?php foreach($buttonsIncluded as $button): ?> <?php if($button->custom_buttons_include): ?> <?php if($buttonsIncludedCountAdditional == 2): ?> <div class="col-6"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php elseif($buttonsIncludedCountAdditional == 3): ?> <div class="col-4"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php elseif($buttonsIncludedCountAdditional == 4): ?> <div class="col-3"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php endif; ?> <?php endif; ?> <?php endforeach; ?> </div> All of this is included in a larger foreach statement that is pulling in other data (like body copy etc etc) from a Page Table field. As you can see in my code above, I am adding "1" to the count, so I can have space in the grid layout for a new button.
      So, right now: it looks something like: 
      [repeater button] [repeater button] [repeater button] [space for new button] What I really need to do is to pull in the button from the Page Table and add it into the new space so it looks like:
      [repeater button] [repeater button] [repeater button] [button from Page Table] Is this even possible todo, or is there a better way to go about this? 
       
      *Edit*
      So, I really just overlooked something quite easy here. Since the grid is based on 12 columns, I could just take 12 and divide by $buttonsIncludedCountAdditional which would give me the remaining col width to use outside the foreach loop. I was trying to make this too complicated.
    • By rolisx
      Hi all
      I have a problem here. I created a gallery with 240 pictures. Created an images field with no maximum amount (0). Unfortunately, only 98 of the 240 images show on the website. Any idea what I possibly could have done wrong? Thanks for your help!
      <?php foreach ($page->images as $image) { $options = array( 'quality' => 90, 'upscaling' => false ); $thumb = $image->size(250, 250, $options); ?> <div class="col-xl-2 col-lg-3 col-md-4 col-sm-6 col-6 foto"> <a href="<?= $image->url ?>" data-lightbox="lightbox" > <img src="<?= $thumb->url ?>" alt="" > </a> </div> <?php } ?>  
    • By celfred
      Hello !
      I have somehting I don't understand here... Here's my code :
      $allPlayers = $pages->find("parent.name=players, team=$selectedTeam"); $allTrains = $allPlayers->find("template=event, task.name~=ut-action, refPage!='', date>=$startDate, date<=$endDate, sort=refPage, sort=date"); bd('$allTrains:'.$allTrains->count()); // DISPLAYS 0 ???? foreach($allPlayers as $p) { $allTrainings = $p->find("template=event, task.name~=ut-action, refPage!='', date>=$startDate, date<=$endDate, sort=refPage, sort=date"); $test += $allTrainings->count(); } bd('$test:'.$test); // DISPLAYS 883 pages (normal) As you can read from my comments, I have no idea why my first $allTrains stays at 0 while the second request actually finds the corresponding pages. If someone could explain I'd appreciate a lot. I have been struggling with this for hours now... For your information, my pages having template 'event' are in a subtree like so :
      - player 01
        - history-1
          - event 01
         - event 02
         - event ...
        - history-2
          - event 01
          - event ...
      - player 02
        - history-1
          - event 01
         - event 02
         - event ...
        - history-2
          - event 01
          - event ...
      - player ...
        - history-1
          - event 01
         - event 02
         - event ...
        - history-2
          - event 01
          - event ...
       
      Thanks in advance. (sorry for my preceding 'tree' which doesn't look like much. I need to find a way to output this better 😉 )
    • By burning
      Hi All, maybe a really stupid question, but is there a way to sort repeater items on -created?
      Like $page->social_activity('limit=10,sort=created'), with 'social_activity' as the repeater field?
      Tried this but it didnt work 😞 
      Any help appreciated! 
×
×
  • Create New...