0

I have an ampscript code and using loops to fetch the data, I'm trying to wrap my table header, when the city list is empty, then, it would only show: No rows found.

However, the header table still showing even when the city is empty. The reason I separate the header and the body so the table header doesn't loop, it should only show once,: ex: name, city,phone and email, while the listing body will loop depend on the data.

My question, in my case what is best practice to wrap HTML table with ampscript?

%%[

set @city = AttributeValue("City")

IF NOT EMPTY(@city) THEN
    set @rows = LookupRows("TEST DE","TestCity", @city)
    set @rowCount = rowcount(@rows)
ELSE
    set @rowCount = 0
ENDIF

]%%

<table width="600">
    <tr>
        <td>Name</td>

        <td>
            City
        </td>
        <td>
            Street
        </td>
        <td>
            Phone
        </td>
        <td>
            Email
        </td>
    </tr>

%%[ if @rowCount > 0 then

for @i = 1 to @rowCount do

    set @row = row(@rows, @i) /* get row based on counter */
    set @Name = field(@row,"FirstName")
    set @TestCity = field(@row,"TestCity")
    set @TestPhone = field(@row,"TestPhone")
    set @TestEmail = field(@row,"TestEmail")

    ]%%

    <tr>
        <td>%%=v(@Name)=%%</td>
  
        <td>%%=v(@TestCity)=%%</td>

        <td>%%=v(@TestPhone)=%%</td>

        <td>%%=v(@TestEmail)=%%</td>
        

%%[ next @i ]%%

</tr></table>

%%[ else ]%%

No rows found

%%[ endif ]%%

1 Answer 1

1

Just move it further down, inside the conditional statement but before the loop - that way the headers will only display if the rowcount is greater than 0:

%%[

set @city = AttributeValue("City")

IF NOT EMPTY(@city) THEN
    set @rows = LookupRows("TEST DE","TestCity", @city)
    set @rowCount = rowcount(@rows)
ENDIF

if @rowCount > 0 then ]%%


<table width="600">
    <tr>
        <td>Name</td>

        <td>
            City
        </td>
        <td>
            Street
        </td>
        <td>
            Phone
        </td>
        <td>
            Email
        </td>
    </tr>


%%[ for @i = 1 to @rowCount do

    set @row = row(@rows, @i) /* get row based on counter */
    set @Name = field(@row,"FirstName")
    set @TestCity = field(@row,"TestCity")
    set @TestPhone = field(@row,"TestPhone")
    set @TestEmail = field(@row,"TestEmail")

    ]%%

    <tr>
        <td>%%=v(@Name)=%%</td>
  
        <td>%%=v(@TestCity)=%%</td>

        <td>%%=v(@TestPhone)=%%</td>

        <td>%%=v(@TestEmail)=%%</td>
        

%%[ next @i ]%%

</tr></table>

%%[ else ]%%

No rows found

%%[ endif ]%%
3
  • Unfortunately, doesn't work, the table gives me an error when I moved it down there
    – Uss
    Jun 21, 2022 at 20:45
  • Apologies, please try now.
    – zuzannamj
    Jun 21, 2022 at 20:46
  • Awesome, it's working, thanks a lot! On the side note, I wouldn't be able to figure it out because I wasn't aware about the brackets open and close, is there documentation or best practice to use html table with ampscript?
    – Uss
    Jun 21, 2022 at 20:56

You must log in to answer this question.

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