I suspect that "allow creation of records" is enabled for the portal.
That gives you one more portal row than you have portal records, the blank "add" row where you enter data to create a new portal record.
Thus, your loop keeps adding a new record in the "add" row and never gets to the last portal row.
If you step through this script in the script debugger (Filemaker Advanced) or put a pause script step right after the set field, you'll see what I am describing. (I've made this mistake myself.)
You could use a counting variable that increments with each loop and an exit loop If stept that compares the variable to the number of related records, but your script would be simpler if you froze the window, used Go to Related Records to pull up the found set of line items on a layout base on line items and then used Replace Field Contents to set the "x" in the Posted field.
Ill do the related records, i already have a layout.