Use range() instead of loop up or down

The function range() handles the case when end value is less than start value. This is used together with array_merge() to append pages independent of the range is increasing or decreasing.