When things don't work, you kind of have to play around with stuff. Code given isn't a solution, but a building block.
Modify this:
$qstring = @eregi_replace("&page=[0-9]{1,3}", "&page=".$next, $qstring);
to be:
$qstring = @eregi_replace("page=[0-9]{1,3}", "page=".$next, $qstring);
and it should work...