in reply to Re^4: Need help with pagination
in thread Need help with pagination

You have to be very careful when building html like this, otherwise you'll keep running into the problems you are posting about now. Separating your code from the HTML using a templating module is a great start, using a modern framework (such as Mojolicious::Lite) to do the heavy lifting for you will save you a lot of time once you get used to it.