24/7 Support: 800.608.6482

Developer Docs

Template Language

Loops

foreach loop

Before we talk about loops, we need to understand one more concept, the array. Think of an array as a group of objects. Sometimes the objects can be products, or sometimes they can be charges on a particular order. Let's think about a category page, every time it loads, the server is going to retrieve a group of products that are assigned to that category, and put them into our array. Once this group of objects are returned, naturally we'd want to access the data (in this case, products) and write them on the page. This is where a foreach loop comes in handy.

What a foreach loop does is take in data from an array, then execute some code for each item in the array.

The code for an foreach loop looks like this:

A standard foreach loop will loop as many times as there are items in the array. If there are 10 products, it will execute 10 times. On pages with pagination (category, search, product list, etc.), there is a field in the admin where you can set the limit on each page. If you have 10 products and limit the loop to 8 per page, a button will appear at the bottom that says "Previous." On that page, you will be able to see the other two products.

In the above sample code you'll notice there is a parameter of "iterator" and one for "array". These change depending on what type of data you are looping through.

The above loop isn't very helpful, normally we'd be writing entities to the page, add to cart buttons, etc. to make this page more useful to the user. A real site might have a foreach loop that looks like this:

The code above retrieves the array for products in the category, then for each product, it will run the code inside of it. So if we have 10 products on our page, the code will run 10 times.

You can get even more advanced control by combining foreach loops with nested if statements.

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.

Accept

Copyright © 1997 – 2023 Miva©, Miva Merchant©, MivaPay©, MivaCon©, Camp Miva ℠, Miva, Inc. All Rights Reserved.