Mivascript: The Wide World of Arrays

Miva Merchant Modules and Development
Arrays are indispensable in the world of coding. Mivascript arrays are easy to use and follow the same basic array principles you'll find in any coding language.

Mivascript: The Wide World of Arrays

A tutorial on how to use arrays in Mivascript


Arrays are indispensable in the world of coding. Mivascript arrays are easy to use and follow the same basic array principles you'll find in any coding language.

by Scot Ranney • July 26, 2019

Miva Scripting 101


Mivascript Arrays on a basic level are a list of items organized in a way that makes them easy to access. All programming languages have some sort of array support and Mivascript is no different.

Filling an Array

Here are a few ways you can add items to an array. For this example we'll use a simple array (list) of names.

The first example is how to create the array manually.

<mvassign name="l.myarray" index="1" value="{ 'Isaac Asimov' }">
<mvassign name="l.myarray" index="2" value="{ 'Ursala K. Leguin' }">
<mvassign name="l.myarray" index="3" value="{ 'Fritz Leiber' }">
<mvassign name="l.myarray" index="4" value="{ 'Andre Norton' }">
<mvassign name="l.myarray" index="5" value="{ 'Fredrich Pohl' }">

The name of the array is l.myarray and we're putting names of sci-fi/fantasy authors into it. The index is what position the array item is in.

Display the Array

One of the most basic things to do with an array is display the array contents. We'll use the built in MvFOREACH Mivascript tag to do this.

<mvforeach iterator="l.arrayitem" index="l.pos" array="l.myarray">
    <mveval expr="{ l.arrayitem }"><br>
</mvforeach>

This displays:

Isaac Asimov
Ursala K. Leguin
Fritz Leiber
Andre Norton
Fredrich Pohl

A more dynamic way of loading an array is to use the miva_array_insert(...) function. It's quite useful while template coding in Miva Merchant templates.

<mvassign name="l.ok" value="{ miva_array_insert(l.myarray,'Isaac Asimov',-1) }">
<mvassign name="l.ok" value="{ miva_array_insert(l.myarray,'Ursala K. Leguin',-1) }">
<mvassign name="l.ok" value="{ miva_array_insert(l.myarray,'Fritz Leiber',-1) }">
<mvassign name="l.ok" value="{ miva_array_insert(l.myarray,'Andre Norton',-1) }">
<mvassign name="l.ok" value="{ miva_array_insert(l.myarray,'Fredrich Pohl',-1) }">

This assigns each name to the end of the array. Instead of -1 you can also manually set a position, however the usefulness of this function is that you don't need to know the position to fill the array. This array will display using the same MvFOREACH code above.

These are the most simple arrays. But what if you want to add more data, for example, specify if the author is a male or female, or the author's favorite ice cream?

Arrays of Structures

This is where structures come in, and it's also quite easy.

Fill the array:

<mvassign name="l.author:name" value="{ 'Isaac Asimov' }">
<mvassign name="l.author:gender" value="{ 'male' }">
<mvassign name="l.author:icecream" value="{ 'Vanilla' }">
<mvassign name="l.ok" value="{ miva_array_insert(l.authors,l.author,-1) }">

<mvassign name="l.author:name" value="{ 'Ursala K. Leguin' }"> <mvassign name="l.author:gender" value="{ 'female' }"> <mvassign name="l.author:icecream" value="{ 'Doomsday Swirl' }"> <mvassign name="l.ok" value="{ miva_array_insert(l.authors,l.author,-1) }">
<mvassign name="l.author:name" value="{ 'Fritz Leiber' }"> <mvassign name="l.author:gender" value="{ 'male' }"> <mvassign name="l.author:icecream" value="{ 'Lankhmar Special' }"> <mvassign name="l.ok" value="{ miva_array_insert(l.authors,l.author,-1) }">
<mvassign name="l.author:name" value="{ 'Andre Norton' }"> <mvassign name="l.author:gender" value="{ 'female' }"> <mvassign name="l.author:icecream" value="{ 'Mint Chocolate' }"> <mvassign name="l.ok" value="{ miva_array_insert(l.authors,l.author,-1) }">
<mvassign name="l.author:name" value="{ 'Fredrich Pohl' }"> <mvassign name="l.author:gender" value="{ 'male' }"> <mvassign name="l.author:icecream" value="{ 'Strawberry' }"> <mvassign name="l.ok" value="{ miva_array_insert(l.authors,l.author,-1) }">

Now each item in the array contains multiple pieces of information. Here's how we display that:

<mvforeach iterator="l.author" index="l.pos" array="l.authors">
    <p>
        Name: <mveval expr="{ l.author:name }"><br>
        Gender: <mveval expr="{ l.author:gender }"><br>
        Ice Cream: <mveval expr="{ l.author:icecream }">
    </p>
</mvforeach>

The output for this is:

Name: Isaac Asimov
Gender: male
Ice Cream: Vanilla

Name: Ursala K. Leguin Gender: female Ice Cream: Doomsday Swirl
Name: Fritz Leiber Gender: male Ice Cream: Lankhmar Special
Name: Andre Norton Gender: female Ice Cream: Mint Chocolate
Name: Fredrich Pohl Gender: male Ice Cream: Strawberry

Bonus: Sorting the Array

Sorting the array should be a basic function of Mivascript, but instead it's a little bit tricky. Not that tricky once you get the hang of it, though. 

Let's sort the above list of authors by their names before we display the data. 

The miva_array_sort( aggregate var, callback, data var) function requires a callback function to do the heavy lifting. I'm not going to get into what this is all about, but this particular callback function will sort on the name. Add this function to the bottom of your script.

<mvfunction name="Sort_Callback" parameters="left var, right var, data var" standardoutputlevel="">
    <mvassign name="l.left_name" value="{ tolower( l.left:name ) }">
    <mvassign name="l.right_name" value="{ tolower( l.right:name ) }">

<mvif expr="{ l.left_name LT l.right_name }"> <mvfunctionreturn value="-1"> <mvelseif expr="{ l.left_name GT l.right_name }"> <mvfunctionreturn value="1"> </mvif>
<mvfunctionreturn value=""> </mvfunction>

Sort the array:

<mvassign name="l.ok" value="{ miva_array_sort(l.authors,'Sort_Callback',l.data) }">

Now when we display the data using the same MvFOREACH loop as above the names will be in alphabetical order.

Full Script

Here's an outline of what the script would look like:

  1. Fill the array
  2. Sort the array
  3. Display the array data
  4. Include callback function
<mvcomment>
|
| fill up array
|
</mvcomment>

<mvassign name="l.author:name" value="{ 'Isaac Asimov' }"> <mvassign name="l.author:gender" value="{ 'male' }"> <mvassign name="l.author:icecream" value="{ 'Vanilla' }"> <mvassign name="l.ok" value="{ miva_array_insert(l.authors,l.author,-1) }">
<mvassign name="l.author:name" value="{ 'Ursala K. Leguin' }"> <mvassign name="l.author:gender" value="{ 'female' }"> <mvassign name="l.author:icecream" value="{ 'Doomsday Swirl' }"> <mvassign name="l.ok" value="{ miva_array_insert(l.authors,l.author,-1) }">
<mvassign name="l.author:name" value="{ 'Fritz Leiber' }"> <mvassign name="l.author:gender" value="{ 'male' }"> <mvassign name="l.author:icecream" value="{ 'Lankhmar Special' }"> <mvassign name="l.ok" value="{ miva_array_insert(l.authors,l.author,-1) }">
<mvassign name="l.author:name" value="{ 'Andre Norton' }"> <mvassign name="l.author:gender" value="{ 'female' }"> <mvassign name="l.author:icecream" value="{ 'Mint Chocolate' }"> <mvassign name="l.ok" value="{ miva_array_insert(l.authors,l.author,-1) }">
<mvassign name="l.author:name" value="{ 'Fredrich Pohl' }"> <mvassign name="l.author:gender" value="{ 'male' }"> <mvassign name="l.author:icecream" value="{ 'Strawberry' }"> <mvassign name="l.ok" value="{ miva_array_insert(l.authors,l.author,-1) }">
<mvcomment> | | sort array | </mvcomment>
<mvassign name="l.ok" value="{ miva_array_sort(l.authors,'Sort_Callback',l.data) }">
<mvcomment> | | display array data | </mvcomment>
<mvforeach iterator="l.author" index="l.pos" array="l.authors"> <p> Name: <mveval expr="{ l.author:name }"><br> Gender: <mveval expr="{ l.author:gender }"><br> Ice Cream: <mveval expr="{ l.author:icecream }"> </p> </mvforeach>
<mvcomment> | | callback function | </mvcomment>
<mvfunction name="Sort_Callback" parameters="left var, right var, data var" standardoutputlevel=""> <mvassign name="l.left_name" value="{ tolower( l.left:name ) }"> <mvassign name="l.right_name" value="{ tolower( l.right:name ) }">
<mvif expr="{ l.left_name LT l.right_name }"> <mvfunctionreturn value="-1"> <mvelseif expr="{ l.left_name GT l.right_name }"> <mvfunctionreturn value="1"> </mvif>
<mvfunctionreturn value=""> </mvfunction>

Related Posts and Information


overall rating:
my rating: log in to rate

mivascript tutorial

The blog posts on Scot's Scripts are made using by Scot's Blogger, a full featured Wordpress replacement Miva Merchant blogging module.