I'm attempting to fill out a table in an email with a DE contact's order info, using AMPscript/HTML. Am working in an HTML block. I have two sets that work in different circumstances but not all. Goal is to send one email, even if there are multiple orders per contact, and break each order into its own table labelled by order number.
Here's the first:
%%[
var @rows, @row, @rowCount, @numRowsToReturn, @contactid, @i, @prevOrderNo
set @contactid = AttributeValue("Contact ID")
set @numRowsToReturn = 0 /* 0 means all */
set @rows = LookupOrderedRows("Order Follow Up", @numRowsToReturn, "Order Number", "Contact ID", @contactid)
set @rowCount = rowcount(@rows)
if @rowCount > 0 then
set @prevOrderNo = ""
for @i = 1 to @rowCount do
var @OrderNo, @itemid, @price, @qty
set @row = row(@rows,@i)
set @OrderNo = field(@row,"Order Number")
set @itemid = field(@row,"Item ID")
set @price = field(@row,"Price")
set @qty = field(@row,"Qty")
if empty(@prevOrderNo) or @prevOrderNo != @OrderNo then
outputline(concat("<br>Order #", @OrderNo, "<br>"))
outputline(concat('<table class="test"><tr>
<td>QTY</td>
<td>ITEM</td>
<td>PRICE</td></tr>'))
set @prevOrderNo = @OrderNo
endif
outputline(concat("<tr><td>", @qty, "</td>"))
outputline(concat("<td>", @itemid, "</td>"))
outputline(concat("<td>", @price, "</td></tr><br>"))
next @i
outputline(concat("</table>"))
else
outputline(concat("<br>No transactionsList rows found"))
endif
]%%
The above works when the recipient has more than one item on an order, but when they have more than one order per email, it breaks down. The tables for both orders get misplaced below the second order number.
Below is the second I tried. Before making you scan it all, note the main difference is the placement of the closing </table>
tag. I suspect this is the culprit but am not sure how else to fix it within constraints of the AMPscript, specifically the loop.
%%[
var @rows, @row, @rowCount, @numRowsToReturn, @contactid, @i, @prevOrderNo
set @contactid = AttributeValue("Contact ID")
set @numRowsToReturn = 0 /* 0 means all */
set @rows = LookupOrderedRows("Order Follow Up", @numRowsToReturn, "Order Number", "Contact ID", @contactid)
set @rowCount = rowcount(@rows)
if @rowCount > 0 then
set @prevOrderNo = ""
for @i = 1 to @rowCount do
var @OrderNo, @itemid, @price, @qty
set @row = row(@rows,@i)
set @OrderNo = field(@row,"Order Number")
set @itemid = field(@row,"Item ID")
set @price = field(@row,"Price")
set @qty = field(@row,"Qty")
if empty(@prevOrderNo) or @prevOrderNo != @OrderNo then
outputline(concat("<br>Order #", @OrderNo, "<br>"))
outputline(concat('<table class="test"><tr>
<td>QTY</td>
<td>ITEM</td>
<td>PRICE</td></tr>'))
set @prevOrderNo = @OrderNo
endif
outputline(concat("<tr><td>", @qty, "</td>"))
outputline(concat("<td>", @itemid, "</td>"))
outputline(concat("<td>", @price, "</td></tr><br></table>"))
next @i
else
outputline(concat("<br>No transactionsList rows found"))
endif
]%%
This one works in reverse: if there are multiple orders in an email, it's great. But if there are multiple items in one order, it fails to place subsequent line items in the same table as the first.