Mivascript 101: The miva_splitstring(..) Function With Examples

Miva Merchant Modules and Development
Use miva_splitstring(..) to quickly take any delineated list and turn it into a Mivascript array. What used to take two or three lines of code now takes one line of code.

Mivascript 101: The miva_splitstring(..) Function With Examples

miva_splitstring(..) is a relatively new Mivascript function and one of the most useful.


Use miva_splitstring(..) to quickly take any delineated list and turn it into a Mivascript array. What used to take two or three lines of code now takes one line of code.

by Scot Ranney • February 13, 2025

Miva Merchant Storemorph, Miva Scripting 101


What is miva_splitstring(..) good for? All sorts of things.

1. Checkboxes or some other multi-selection with the same name in a form.

Here's a super basic example of giving your customer a range of checkboxes to choose from, such as favorite colors:

<p>Select your favorite colors:</p>
<form>
	<input type="checkbox" name="colors" value="red"> Red
	<input type="checkbox" name="colors"value="red"> Green
	<input type="checkbox" name="colors"value="red"> Yellow
	<input type="checkbox" name="colors"value="red"> White
	<input type="checkbox" name="colors"value="red"> Black
	<button type="submit">Submit</button>?
</form>

If the customer selects the red checkbox the value g.colors will be red however if the customer selects red, green, and yellow, the g.colors value will be red,green,yellow

Let's say we want to display these values in a Miva Merchant template after the form is submitted, and there are several ways to do this.

The most inefficient way is to have a bunch of mvt:if statements such as:

<mvt:if expr="g.color EQ 'red'">
	<p>You selected red!</p>
</mvt:if>
<mvt:if expr="g.color EQ 'white'">
	<p>You selected white!</p>
</mvt:if>

Using this method is simple to code but requires maintenance if the color options change or you want to change the general message, such as changing "You selected..." to "You chose..."

The most efficient way of doing this is to turn the g.colors variable into an array and then loop through the selected values. To do this we use miva_splitstring(..) to turn g.colors into an array:

<mvt:assign name="l.ok" value="miva_splitstring(g.colors,',',l.settings:colors,'trim')" />

<mvt:foreach iterator="color" array="colors">
	<p>You selected &mvte:color;</p>
</mvt:foreach>

Using this method handles 1 or more selections without any issue and if you want to add colors to the your checkbox list that's all your have to do. The only time you need to change the "You selected.." section is if you want to change the words to something such as "You chose..." 

2. Miva Merchant Custom Field Lists

How many of us have used custom fields to hold a list of product codes or some such thing?

Let's say we have a "suggested products" custom field where we list product codes separated by spaces. After we load the custom field into say l.products the value will be something like this:

pcode1 pcode2 pcode3 pcode4

Now let's say we want to load the information for these products into our own product array:

<mvt:assign name="l.ok" value="miva_splitstring(l.product_codes,' ',l.settings:pcodes,'trim')" />

<mvt:foreach iterator="pcode" array="pcodes">
	<mvt:comment> ## it is always a good idea to clear a temp variable like this before loading new product (or whatever) data into it ## </mvt:comment>

	<mvt:assign name="l.temp_product" value="''" />
	<mvt:do file="g.Module_Library_DB" name="l.ok" value="product_load_code(l.settings:pcode, l.temp_product)" />
	<mvt:assign name="l.ok" value="miva_array_insert(l.settings:suggested_products,l.temp_product,-1)" />
</mvt:foreach>

Now we have an array of basic product information in l.settings:suggested_products that we can loop through and do whatever we want with.j

Pro Tip:

In order to easily use this array it would be a good idea to add formatted prices and URIs if you need the product links. Add a few more function calls in the previous loop to add this data before adding it to the suggested products array:

<mvt:assign name="l.ok" value="miva_splitstring(l.product_codes,' ',l.settings:pcodes,'trim')" />

<mvt:foreach iterator="pcode" array="pcodes">
	<mvt:comment> ## it is always a good idea to clear a temp variable like this before loading new product (or whatever) data into it ## </mvt:comment>

	<mvt:assign name="l.temp_product" value="''" />
	<mvt:do file="g.Module_Library_DB" name="l.ok" value="product_load_code(l.settings:pcode, l.temp_product)" />
	<mvt:do file="g.Module_Store_Module_Currency" name="l.settings:temp_product:formatted_price" value="CurrencyModule_AddFormatting(l.settings:module, l.temp_product:price)" />
	<mvt:do file="g.Module_Store_Module_Currency" name="l.settings:temp_product:formatted_base_price" value="CurrencyModule_AddFormatting(l.settings:module, l.temp_product:base_price)" />
	<mvt:do file="g.Module_Feature_URI_DB" name="l.ok" value="URI_Load_Product_Canonical(l.temp_product:id, l.temp_product:uri)" />

	<mvt:assign name="l.ok" value="miva_array_insert(l.settings:suggested_products,l.temp_product,-1)" />
</mvt:foreach>

Now you will have your formatted prices and you'll find the product URI in suggested_products:uri:uri - you can also load images and other stuff in it.


overall rating:
★★★★★
★★★★★
★★★★★
my rating: log in to rate

mivascript stormorph

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