You're overcomplicating things a lot.
Don't even worry about the looping aspect.
You can simplify your problem quite a lot.
For example, you'd somehow determine that for your current x and y position you'd have to draw the tile at coordinates
tile_x and
tile_y[/‹].
So to determine the actual tile to draw (including loops):
real_tile_x = tile_x % width_in_tiles;
real_tile_y = tile_y % height_in_tiles;
So let's assume your map is 10 tiles wide (index 0-9) and you'd like to draw the tile at offset 11 (second repeating tile), so you'd end up with:
real_tile_x = 11 % 10;
Of course, the result of this is 1, which represents the second tile/column. Job done!