move the code that sets the $curOrder variable to 0 at lines 435 - 437:
<?php
$curOrder = 0;
?>
to line 417 so it is before the recordset loop:
<?php
$curOrder = 0;
?>
<?php do { ?>
by having it inside the recordset loop, it is reseting to 0 on each record.