How do I display different HTML depending on if fields have values? (aka a loop)

91 Views Asked by At

I have a Data Extension with fields like Email 1, Email 2, ... , Email 10 and Phone 1, Phone 2, ... , Phone 10. Every contact will have a value for Email 1, but the rest could vary (e.g. Email 1, Phone 1, and Phone 2 OR Email 1, Email 2, Email 3, Phone 1 etc). The body of my email has 2 columns one for email and one for phone. What AMPscript could I use to create a loop (I'm assuming this is the correct approach) to check for all of the values and only show them if they are not null.

1

There are 1 best solutions below

0
Frogmouth On

You can try something like this:

<table>
%%[
    FOR @counter = 1 TO 10 DO

    SET @email = AttributeValue(CONCAT('Email ', @COUNTER))
    SET @phone = AttributeValue(CONCAT('Phone ', @COUNTER))
]%%
    %%[IF NOT EMPTY(@email) OR NOT EMPTY(@phone) THEN]%%
    <tr>
        <td>Email %%=v(@COUNTER)=%%: %%=IIF(empty(@email),'-',@email)=%%<td>
        <td>Phone %%=v(@COUNTER)=%%: %%=IIF(empty(@phone),'-', @phone)=%%<td>
    </tr>
    %%[ENDIF]%%
%%[ NEXT ]%%
</table>

enter image description here