Перейти к содержимому

GetRows

GetRows method retrieves a number of rows from specified custom data table, according to specified criteria. This is an equivalent of SQL SELECT query.

Different parameters allow specifying what table is used, how many rows are returned, what criteria they must match, sorting etc.

This method may be useful, if you want to display custom data in emails or do some conditional formatting that depends on it.

1. Method signature

rows = GetRows(table, top, columns, filters, order, filterOperator)

Parameter definition

NameTypeDescription
tablestringRequired. Table name.
topintOptional. Maximum number of records that will be retrieved. If set to null, this setting will default to 500. If set to more than 500, this setting will be ignored.
columnsarray of stringOptional. Array of columns that will be retrieved. If set to null, all columns will be retrieved.
filtersarray of Filter objectsOptional. Array of objects defining elements of WHERE phrase of SELECT query. Use this parameter, to restrict selected rows to those matching your criteria. If null, no criteria will be applied. If several filters are defined, by default returned rows will match all of them (equivalent to logical AND). Default behavior can be overridden using filterOperator parameter.
orderarray of Order objectsOptional. Array of objects defining sorting of returned rows (equivalent to ORDER BY). If null, rows will not be sorted (will be returned in order supplied by the database). If several order objects are defined, table will be multi-sorted.
filterOperatorenumerationOptional. Logical operator used for filters. Can be one of the following:AND (filter1 AND filter2 AND filter3…) — this is the default valueOR (filter1 OR filter2 OR filter3…)ANDNOT (NOT filter1 AND NOT filter2 AND NOT filter3…)
rowsarray of rowsReturn value. Array of rows. Every row itself is an array of key-value pairs (column name => column value). Column name is string. Column value type depends on actual type in the database. Array is empty if no data was found.
Rows table can be enumerated upon, e.g. to display in form of HTML table. See examples for more information

Filter object parameter definition

NameTypeDescription
NamestringColumn name
ModiferenumerationOperation modifier. Can be one of the following:EQ (equals, «=»)GT (greater than, «>»)LT (lower than, «<«)LIKE (SQL LIKE operator)
ValueobjectColumn value. Value type depends on column type, e.g. when filtering by number column, integer value should be supplied, string for text columns etc. Simple type conversion is also supported, e.g. string «123» representing correct integer value 123 will be accepted. This also works for dates, floating point numbers etc.

Order object parameter definition

NameTypeDescription
NamestringColumn name
DescenumerationOrder direction. Can be one of the following:ASC (ascending, e.g. oldest forst for dates, alphabetical sorting for texts, smallest first for numbers etc)DESC (descending, e.g. newest first for dates, reverse alphabetical sorting for texts, biggest first for numbers etc)

NOTE: Typically, Filter and Order objects are created using constructors, which is more convenient — see examples for more information.

2. Using values returned by GetRows method

Values returned by GetRows method are arrays (enumerables). This means they are not simple values that can be displayed directly (such as ${SubscriberEmail}), but complex values that must be processed first to access individual column values. There are several ways to do it, the most common are enumerating and accessing directly by index.

2.1. Enumerating rows and columns

Enumerating is going through all values in an array without the need to know how many values it contains. This is useful if we do not know e.g. how many records will be returned or how many columns there are in a single record.

The easiest way to enumerate is to use «each» attribute. This attribute can be applied to any element and will duplicate it as many times as the enumerated array contains elements. In case of GetRows, it may be the number of rows or the number of columns in specific row.

Example — Simple table with contents read from database. <tr> elements are created from enumerating rows. <td> elements are created from enumerating columns in a single row.

<table>
  <tr each="var row in GetRows('MyTable')">
    <td each="var column in row">
      ${column.Value}
    </td>
  </tr>
</table>

2.2. Accessing rows and columns directly

Rows and columns can be accessed directly using array indexing. Rows are indexed by numbers (starting from 0) and columns are indexed by their names.

This method requires knowing exactly what the returned array contains. If wrong index is used (e.g. column name is misspelled or row index is out of bounds), it will result in error.

Example — Displaying contents of 3 rows read from the database. GetRows result is stored in a variable and each row is accessed directly using index.

<var rows="GetRows('MyTable', 3)"/>
<div>Row no 1: ${rows[0]['MyColumn']}</div>
<div>Row no 2: ${rows[1]['MyColumn']}</div>
<div>Row no 3: ${rows[2]['MyColumn']}</div>

2.3 Mixing access methods

Of course, both methods can be mixed if necessary.

Example — List with contents read from database. <li> elements are created from enumerating rows. Each row is created by accessing columns directly by their names.

<ul>
  <li each="var row in GetRows('MyTable')">
    <a href="mailto: ${row['Email']}">${row['Firstname']} ${row['Lastname']}</a>
  </li>
</ul>

3. Using GetRows parameters to create more complicated requests

Below you can find several examples of calling GetRows with additional parameters. These calls can be inserted into previous examples to create a complete email template.

Example 1 — The most simple call

All records from specified table are returned. No additional criteria.

GetRows('MyTable')

Equivalent SQL query:

SELECT * FROM MyTable

Example 2 — Selecting at most 5 records and only specified columns.

GetRows('MyTable', 5, new [] {'Firstname', 'Lastname', 'Email'})

Equivalent SQL query:

SELECT TOP 5 Firstname, Lastname, Email FROM MyTable

Example 3 — displaying all rows matching specific criteria. E.g. all entries connected to specific customer with some ID.

GetRows('MyTable', null, null, new [] {new Filter('CustomerId', EQ, 1234)})

Equivalent SQL query:

SELECT * FROM MyTable WHERE CustomerId = 1234

Example 4 — using multiple filters.

GetRows('MyTable', null, null, new [] {new Filter('CustomerId', EQ, 1234), new Filter('Price', GT, 24.99)})

Equivalent SQL query:

SELECT * FROM MyTable WHERE CustomerId = 1234 AND Price > 24.99

Example 5 — using subscriber properties in filters

GetRows('MyTable', null, null, new [] {new Filter('CustomerId', EQ, SubscriberProperty('customer_id'))})

Equivalent SQL query:

SELECT * FROM MyTable WHERE CustomerId = @customer_id

NOTE: @customer_id is a parameter that will be filled with actual subscriber property value.

Example 6 — Using filterOperator

GetRows('MyTable', null, null, new [] {new Filter('Color', EQ, 'red'), new Filter('Color', EQ, 'blue')}, null, OR)

Equivalent SQL query:

SELECT * FROM MyTable WHERE Color = 'red' OR Color = 'blue'

Example 7 — displaying 10 most recent records (sorted by date column)

GetRows('MyTable', 10, null, null, new [] {new Order('CreationDate', DESC)})

Equivalent SQL query:

SELECT TOP 10 * FROM MyTable ORDER BY CreationDate DESC

Example 8 — Multisorting

GetRows('MyTable', null, null, null, new [] {new Order('CreationDate', DESC), new Order('ProductName', ASC)})

Equivalent SQL query:

SELECT * FROM MyTable ORDER BY CreationDate DESC, ProductName ASC
On this page
To top