1

I am trying to set a loop organizing the result in 2 columns. However, since the @cnt variables cannot be re-set, I am quite stacked. Scenario: from a field in DE containing XML, I am extracting the SKU_id of the products, and I would like to show them organized in a 2 column layouts. Please find here my attempt:

%%[
Set @ROWS_SKU_ID = BuildRowsetFromXML(@HTML_XML,"//SKU_ID/value",1)
Set @count_SKU_ID = RowCount(@ROWS_SKU_ID)
]%% 
RowCount: %%=v(@count_SKU_ID)=%%<br>
%%[
for @cnt = 1 to @count_SKU_ID do
]%% 
column 1: %%=v(Field(Row(@ROWS_SKU_ID,@cnt),"Value"))=%%
column 2: %%=v(Field(Row(@ROWS_SKU_ID,@cnt),"Value"))=%%
%%[
NEXT @cnt 
]%% 

However, this is going to re-iterate on both columns the same value. Any ideas?

Edit. I tried to use the MOd() function, as suggested from @jeff bennet. However, it seems it doesn't consider the Mod() function, because performing the email the sku id are organized in one-column layout.

This was my attempt:

%%[
Set @ROWS_SKU_ID = BuildRowsetFromXML(@HTML_XML,"//SKU_ID/value",1)
Set @count_SKU_ID = RowCount(@ROWS_SKU_ID)
]%% 
RowCount: %%=v(@count_SKU_ID)=%%<br>

%%[
for @cnt = 1 to @count_SKU_ID do
]%% 



              <table bgcolor="%%=v(@color_white)=%%" align="center" cellpadding="0" cellspacing="0" width="100%">
                 <tbody>
                    <tr>

  %%[
  IF MOD(@cnt,2) == 0 then 
  ]%% 


                       <td width="260" align="left" colspan="3" bgcolor="#ffffff" style=""><span>%%=v(Field(Row(@ROWS_SKU_ID,@cnt),"Value"))=%%</span></td>


  %%[ELSE]%%

                       <td width="260" align="left" colspan="3" bgcolor="#ffffff" style=""><span>%%=v(Field(Row(@ROWS_SKU_ID,@cnt),"Value"))=%%</span></td>


  %%[ENDIF]%%

                    </tr>
                 </tbody>
              </table>



 %%[
 NEXT @cnt 
 ]%%      

2 Answers 2

2

My AMPscript isn't super strong, but it should be as simple as having a IF statement and the using the MOD function to find if @cnt is odd or even.

Something like

%%[IF MOD(@cnt, 2) == 0 THEN]%%
    column 1: %%=v(Field(Row(@ROWS_SKU_ID,@cnt),"Value"))=%%
%%[ELSE]%%
    column 2: %%=v(Field(Row(@ROWS_SKU_ID,@cnt),"Value"))=%%
%%[ENDIF]%%

More information about MOD can be found here or here.

1
  • Hi Jeff, I already tried it, but it seems the %%[ELSE]%% condition is not evalueted. Find blow my code. Trying to perform it, it displays the values, but in one-column layout, and analyzing the code you can see each value is wrapped in a table (opening and closing tag of the min element of the iteration). Jul 10, 2018 at 16:28
0

I was able to obtain the desidered result. The function proposed by @Jeff Bennett was on the right track (and I already tried to apply it), but you have to play around with HTML. Important hint: keep in mind that IF conditions allow you to display one option OR another one. Hope it helps!

Here find below my code:

                %%[
                    Set @ROWS_SKU_ID = BuildRowsetFromXML(@HTML_XML,"//SKU_ID/value",1)
                    Set @count_SKU_ID = RowCount(@ROWS_SKU_ID)
                    ]%% 



                    %%[
                    for @cnt = 1 to @count_SKU_ID do
                    ]%% 
                    %%[
                    IF MOD(@cnt,2) ==1 THEN SET @align = "left"
                    ELSEIF MOD(@cnt,2) ==0 THEN SET @align = "right"
                    ENDIF
                    ]%% 

                    <table align="%%=v(@align)=%%" border="0" cellpadding="0" cellspacing="0" width="50%" style="max-width:260px">
                          <tbody>
                                <tr>
                                      <td bgcolor="%%=v(@color_white)=%%">
                                      <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                            <tbody>
                                                  <tr>
                                                        %%[IF MOD(@cnt,2) ==1 THEN]%%
                                                        <!--[if (gte mso 9)|(IE)]>
                                                        <td width="299">
                                                        <![endif]-->
                                                        <td width="100%" valign="top" style="">ODD CNT: %%=v(@cnt)=%%<br><br>
                                                        </span>
                                                        </td>
                                                        <!--[if (gte mso 9)|(IE)]>
                                                        </td>
                                                        <![endif]-->
                                                    %%[ELSEIF MOD(@cnt,2) ==0 THEN]%%

                                                        <!--[if (gte mso 9)|(IE)]>
                                                        <td width="299">
                                                        <![endif]-->
                                                        <td width="100%" valign="top" style="">EVEN CNT: %%=v(@cnt)=%%<br><br>
                                                        </span>
                                                        </td>
                                                        <!--[if (gte mso 9)|(IE)]>
                                                        </td>
                                                        <![endif]-->
                                                       %%[ELSE]%%
                                                      %%[ENDIF]%%
                                                  </tr>
                                            </tbody>
                                      </table>
                                      </td>
                                </tr>
                          </tbody>
                    </table>


                    %%[
                    NEXT @cnt
                    ]%% 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .