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 ]%%