fixed undefined offset -> parameterized loop in where statement, orderby is using correct index now