Day 15 – Show dynamic content with For loop

Challenge

https://ampscript30.com/ampscript-challenge-day15/

To display dynamic content using For loop.

Solution

%%[
SET @fullname = Concat(FirstName, " ", LastName)
SET @mileage = Mileage
SET @rows = LookupRows("AMPscript - Day 15 Services", "Mileage", @mileage)
SET @rowcount = RowCount(@rows)
]%%
Hi %%=v(@fullname)=%%,

As we enjoy the remaining days of summer, its time to think about getting your vehicle ready for fall adventures and winter road trips. Now is a great time to check major components like tires, brakes and batteries to help give you confidence on the roads as the days get shorter and the weather begins to change. 

Here are recommended services:
%%[
IF @rowcount > 0 THEN
     FOR @i = 1 TO @rowcount DO
         SET @row = Row(@rows,@i)
         SET @name = Field(@row, "Name")
         SET @description = Field(@row, "Description")
     ]%%     
%%=v(@name)=%%: %%=v(@description)=%%
     %%[NEXT @Variable
ENDIF]%%
Visit your nearby Next Auto dealer for all your vehicle needs.

Best regards,
Next Auto

Preview

Summary

For loop syntax:

%%[FOR @Variable = <start expression> TO/DOWNTO <end expression> DO ]%%
        [wrapped script or email content]
%%[NEXT @Variable]%%

The <start expression> should be the start counter (usually set to 1), and the <end expression> should be the end counter (usually set another variable to the total count of rows and use it as the end counter).

Since the logic doesn’t include an ELSE statement, it’s a good practice to use the RaiseError function to handle the exception when some subscribers haven’t reached the minimum mileage that requires the services yet. MC will return the pre-defined error message and stops the send to that subscriber only.