24/7 Support: 800.608.6482


Developer Videos

Videos | Pos1 – Loop Counter

ver. 9.0 and later

Video Transcript

Now having just been introduced to loops within the Miva Merchant template language, both foreach loops and while loops. There’s one more programming construct that will make your life easier. It’s call pos1 or position 1. Pos1 is a built in loop counter. It’s native to all foreach loops and while loops. It’s a special local variable that will give you the current iteration of the loop that you’re on. It’s typically just written or referred to as pos1 but l.pos1 also works and you’ll see them written both ways.

One other feature of pos1 is that when you have nested loops, so say you have one foreach loop inside of another foreach loop, the pos1 variable changes from pos1 to pos2, pos3, etc. However many loops you have, that number increments. So the 1 just refers to the loop you’re in, the two the 2nd and so on. Most the time when using the position variable you’ll use just pos1. However, it’s important to know that the other ones exist. Let’s take a look at some examples. So here I’m back at my text editor. I grabbed a foreach loop from the category page. So this is the loop that outputs products that are assigned to a category. It’s part of the category listing item and it’s the products array. Here my iterator is “product.” So this loop will loop through every product assigned to this category and output some value. For this example I got rid of all the code and I’m just outputting pos1. I’m using mvt:eval and I’m putting l.pos1. I could get rid of the l. and just do the pos1 and both should output the same value. So let’s bring this code over to Miva and let’s see what happens when it runs. In Miva I’m on the CTGY page and I’m just going to put this in the Header and Footer section of this page, click “Update.” When I come back to the category page on the front end and hit refresh you’ll see it outputs Pos1 and it has the Values 1-10. That’s exactly what we’d expect because there’s ten products on this page.

Let’s take a look at another example. So here I added a new “if” statement. If pos1 EQ 5 and again, I could put l.pos1 here or just use pos1. So if it does equal five, it will output this comment, “This is the 5th iteration of the loop.” So we should see this comment one time after the fifth iteration of the loop. Let’s run it and see what happens. Now when I come back to the store and refresh, you’ll see there’s my comment and it’s right after the fifth iteration of the loop. So this is a great way to target a specific iteration within a foreach loop.

Now what happens if you want to target multiple within the foreach loop? Say every fourth, eighth, twelfth, sixteenth product within that array. Well, there’s a great math trick that allows us to do this. We can use pos1 with the MOD operator, which is the modulus operator. This will return the remainder after it divides two numbers together. So what it will do is it will take pos1 the value, divide it by 4 and if the remainder equals 0, which means if it evenly divides by that number, then it will return true. So here, if pos1 equals 4, 4 divided by 4 is one with the remainder of 0 so that would be true. The same is true with 8. 8 divided by 4 is 2 with the remainder of 0 and that would return true as well. Let’s run this and see what happens. I’ll paste this in here and if I refresh this on the frontend, so you’ll see after the fourth iteration of the loop, it prints this comment and then after the eighth loop it prints it again. If we had twelve or sixteen it would also do it. This is a great little tool to allow you to target different multiples within the loop.

So the last thing I want to show you has to do with the nested foreach loops. When to use pos1 versus pos2, pos3, etc. I’m going to get rid of this code that we were working with earlier and I’m going to paste in the same foreach loop again so we have nested foreach loops. So it’s the same category_listing_products, and I changed the iterator to product2 just so it’s unique. Here, I have Pos2 Value: and I’m outputting l.pos2 and it’s pos2 because it’s the second loop. This loop is contained within the previous loop. Say I had another loop within this foreach loop then the pos counter would get incremented into pos3. So let’s run this and see what outputs. So here we see it has the pos1 value of 1 and then it gets into the second foreach loop and within that foreach loop it loops through it ten times and it outputs one through ten again. Then it comes back to pos1 as a value of 2 and it loops through the pos2 value ten times again. This is exactly what we would expect.

So the pos1 counter is a great built in feature and it will save you from having to create and maintain your own counters when working with loops.

Example Code

Reference Documentation

Looking for Developer Docs?

We have a whole section for that, including: Developer Training Series, Template Language docs, Module Development tutorials and much, much more.

Head to the Developer Section

This website uses cookies to identify visitors, track visitors to our website, store login session information and to remember your user preferences. By continuing to use this site you agree to our use of cookies. Learn More.

This website uses cookies. By continuing to use this site you agree to our use of cookies. Learn More.


Copyright © 1997 – 2024 Miva®, Miva Merchant®, MivaPay®, MivaCon®, Camp Miva®, Miva Connect®, Miva, Inc. All Rights Reserved.