0

I have an issue with my 'for' loop statement. It is producing duplicate content. My loop statement is as follows:

/* look up top 3 destinations from preferred airport in EU only if preferred continent is EU */
%%[
IF @PreferredContinent_EU == true AND @PreferredDepartureCity_AMS == true then

/* lookup up top 3 from Amsterdam, only if preferred airport is Amsterdam */
    set @rows_EU_AMS = LookupOrderedRows("DE_Top_Destinations_City", @rows_to_return, "Rank DESC", "DepartureCity", "Amsterdam")
    set @rowcount_EU_AMS = RowCount(@rows_EU_AMS)

/* get city and price in EU city from Amsterdam position number 1 */
          for @i = 1 to RowCount(@rows_EU_AMS) do
          Set @row1AMSEU = Row(@rows_EU_AMS, @i)
            var @destination_eu_city_AMS_1, @destination_eu_price_AMS_1, @destination_eu_url_AMS_1
              set @destination_eu_city_AMS_1 = field(@row1AMSEU, "DestinationCity")
              set @destination_eu_price_AMS_1 = field(@row1AMSEU, "Price")
              set @destination_eu_url_AMS_1 = field(@row1AMSEU, "LandingsURL")]%%

<div> Your flight from Amsterdam to %%=v(@destination_eu_city_AMS_1)=%% %%=v(@destination_eu_price_AMS_1)=%% <a href="%%=v(@destination_eu_url_AMS_1)=%%">checkout here</a>

%%[NEXT @i]%%
          %%[for @i = 2 to RowCount(@rows_EU_AMS) do
              Set @row2AMSEU = Row(@rows_EU_AMS, @i)
            var @destination_eu_city_AMS_2, @destination_eu_price_AMS_2, @destination_eu_url_AMS_2
              set @destination_eu_city_AMS_2 = field(@row2AMSEU, "DestinationCity")
              set @destination_price_AMS_2 = field(@row2AMSEU, "Price")
              set @destination_eu_url_AMS_2 = field(@row2AMSEU, "LandingsURL")]%%

%%=v(@destination_eu_city_AMS_2)=%% %%=v(@destination_eu_price_AMS_2)=%% <a href="%%=v(@destination_eu_url_AMS_2)=%%">checkout here</a> <br>

%%[NEXT @i]%%
              %%[for @i = 3 to RowCount(@rows_EU_AMS) do
              Set @row3AMSEU = Row(@rows_EU_AMS, @i)
                var @destination_eu_city_AMS_3, @destination_eu_price_AMS_3, @destination_eu_url_AMS_3
                  set @destination_eu_city_3 = field(@row3AMSEU, "DestinationCity")
                  set @destination_eu_price_3 = field(@row3AMSEU, "Price")
                  set @destination_eu_url_3 = field(@row3AMSEU, "LandingsURL")]%%

%%=v(@destination_eu_city_AMS_3)=%% %%=v(@destination_eu_price_AMS_3)=%%  <a href="%%=v(@destination_eu_url_AMS_3)=%%">checkout here</a><br>

%%[NEXT @i]%%
%%[ENDIF]%%

instead of rendering three times Destination city, Price and URL, it shows up six times.

Anyone who could help me out?

Thanks

2 Answers 2

1

I am assuming that RowCount(@rows_EU_AMS) is coming back as 3.

On that assumption, since you are running the for loop 3 times, just adjusting the iterative starting number - this is why you are getting more than 3 results.

Look at it this way.

Ending value (Rowcount) = 3.

For loop 1: Start at 1 | End at 3 | Total runs: 3
For loop 2: Start at 2 | End at 3 | Total runs: 2
For loop 3: Start at 3 | End at 3 | Total runs: 1
Total Loop runs: 6

So according to your code, it is asking for you to loop through those 3 rows a total of 6 times.

With slight adjustments, you can make it work:

%%[
/* look up top 3 destinations from preferred airport in EU only if preferred continent is EU */
IF @PreferredContinent_EU == true AND @PreferredDepartureCity_AMS == true then

/* lookup up top 3 from Amsterdam, only if preferred airport is Amsterdam */
    set @rows_EU_AMS = LookupOrderedRows("DE_Top_Destinations_City", @rows_to_return, "Rank DESC", "DepartureCity", "Amsterdam")
    set @rowcount_EU_AMS = RowCount(@rows_EU_AMS)

/* get city and price in EU city from Amsterdam position number 1 */
  for @i = 1 to RowCount(@rows_EU_AMS) do
    Set @row1AMSEU = Row(@rows_EU_AMS, @i)
    var @destination_eu_city_AMS, @destination_eu_price_AMS, @destination_eu_url_AMS
    set @destination_eu_city_AMS = field(@row1AMSEU, "DestinationCity")
    set @destination_eu_price_AMS = field(@row1AMSEU, "Price")
    set @destination_eu_url_AMS = field(@row1AMSEU, "LandingsURL")]%%

    if @i == 1 then
      <div> Your flight from Amsterdam to %%=v(@destination_eu_city_AMS)=%% %%=v(@destination_eu_price_AMS)=%% <a href="%%=v(@destination_eu_url_AMS)=%%">checkout here</a>
    else
      %%=v(@destination_eu_city_AMS)=%% %%=v(@destination_eu_price_AMS)=%% <a href="%%=v(@destination_eu_url_AMS)=%%">checkout here</a> <br>
    endif

  NEXT @i

ENDIF
]%%

The above should get you the results you want, based on my assumptions of your ask.

1
  • Thank you for your answer. This makes it a lot more clear. Is it however possible with your adjustment to return rows 1 till 3, with unique variables per row? The goal is to return the top 3 destinations from departure airport AMS. Hence I am declaring the variables (..)_ams_1, (..)_ams_2 and (..)_ams_3. Oct 31, 2019 at 9:56
0

So the goal was to return three rows from a data extension, and then output each row separately to be able to define variables based on values of columns from the three rows separately. With Gortington's answer, I was able to define a solution.

What I was missing was the Row() function. First I used the LookupOrderedRows() functions to ouput the first three rows from 'DE_Top_Destinations_City', then I used the Row function to output the first row from the output of LookupOrderedRows.

The code ended up looking like this:

set @rows_EU_AMS = LookupOrderedRows("DE_Top_Destinations_City", @rows_to_return, "Rank DESC", "DepartureCity", "Amsterdam")
    set @rowcount_EU_AMS = RowCount(@rows_EU_AMS)

/* get city and price in EU city from Amsterdam position number 1, 2 and 3 */
          for @i = 3 to RowCount(@rows_EU_AMS) do
          /* get row 1 from lookup */
          Set @row1AMSEU = Row(@rows_EU_AMS, 1)
            var @destination_eu_city_AMS_1, @destination_eu_price_AMS_1, @destination_eu_url_AMS_1, @destination_eu_city_AMS_2, @destination_eu_price_AMS_2, @destination_eu_url_AMS_2,  @destination_eu_city_AMS_3, @destination_eu_price_AMS_3, @destination_eu_url_AMS_3
              set @destination_eu_city_AMS_1 = field(@row1AMSEU, "DestinationCity")
              set @destination_eu_price_AMS_1 = field(@row1AMSEU, "Price")
              set @destination_eu_url_AMS_1 = field(@row1AMSEU, "LandingsURL")
          /* get row 2 from lookup */
          Set @row2AMSEU = Row(@rows_EU_AMS, 2)
            set @destination_eu_city_AMS_2 = field(@row2AMSEU, "DestinationCity")
            set @destination_eu_price_AMS_2 = field(@row2AMSEU, "Price")
            set @destination_eu_url_AMS_2 = field(@row2AMSEU, "LandingsURL")
          /* get row 3 from lookup */
          Set @row3AMSEU = Row(@rows_EU_AMS, 3)
            set @destination_eu_city_AMS_3 = field(@row3AMSEU, "DestinationCity")
            set @destination_eu_price_AMS_3 = field(@row3AMSEU, "Price")
            set @destination_eu_url_AMS_3 = field(@row3AMSEU, "LandingsURL")

You must log in to answer this question.

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