Skip to main content

Creating tables based on array elements

Description

Creating tables based on array elements is one of platform features you can use if you transfer dynamic content. We can take an array of line objects from the database, and then distribute the elements of this array in the table as the situation requires.

tip

This feature is available when transferring content in any way: API content, JSON content, content from a SQL database.

If you want to create tables based on array elements, in the HTML version tab click: Insert variable → Output functionsColumns from array.

The code example will appear in the code editor window. You can work with it.

Look at the line of code:

{for $i $row = columns(array["item1" "item2" "item3" "item4" "item5 hidden"] 2 0)}

Array is an object from which we take content for our table. There is content for the certain table ceil in every element of the array ("item1", "item2", "item3" и etc).

"2" — it says how many columns there are in the table. "0" — it's the amount of elements that will not be added to the table (we count from the first element of the array).

As a result, in our table all elements of the array will be distributed into two columns and none of them will be omitted.

Use cases

Let's take a look at one of the cases for this option.

For instance, an airline's website dynamically changes content for each user. The content depends on the history of his flights. Each user can see on his screen 5 flight offers that he may be interested in. These offers are shown in the interface as 5 blocks (item). Each of them has information on cities, dates and ticket prices. We want to not only display these blocks, but also add a separate block between them, where we will offer the user to choose the city of flight himself (offer):

Item1

Offer

Item2Item3
Item4Item5

Our steps:

  • Create an array where we add information about all five offers ("item1", "item2", "item3", "item4", "item5").

  • Separately add the first element of the array (item1).
  • Add a block with an offer.
  • Add the remaining elements of the array, distributing them into 2 columns. The first element will not be added because we indicated that we want to skip one element.

So, creating tables based on array elements is a flexible mechanism that allows you to easily change the way how the user sees your message.