The code I have works absolutely perfectly:

```
<?
$maxresults = 10;
$total = $row[total];  
$pagecount = $total / $maxresults; 
 if (!isset($_GET['page'])) { $_GET['page'] = '1'; } 
$startcount = (($_GET['page'] - 1) * $maxresults + 1); 
$stopcount = $startcount + ($maxresults - 1);
$lastTime = null;
$i='0';
$sc = $startcount; 
$stmt=$db->prepare("// SELECT STATEMENT");
 $stmt->bindParam(':user', $username);
 $stmt->execute();

     while ( $row = $stmt->fetch() ) {
        if (!($sc > $stopcount)) {
            $i++;
            if( $row['time'] !== $lastTime ) { 
                if ( $lastTime === NULL ) {
                   echo '// close of loop'; 
                } else { 
                   echo '// loop grouping as title'; 
                }
                $lastTime = $row['time'];
            } 

            if ($i >= $sc) { ?>
               // the html to loop
            <?   
               $sc++;
            }  
        }
    }
?>

```

Forward and Back buttons are easy enough to create. if $\_GET['page'] \* 10 > $total, active the next button, if $\_GET['page'] > 1 activate previous button.

What I can't figure out is the correct formula and loop so I can always display the correct number of "page numbered" links. I want to always show 5 based on the 3rd number. So 1 would should 5 links: 1 - 5, 2 would also show 1 - 5, as would 3, 4 would show 2 - 6, 5 would show 3 - 7. 

Now obviously, I could just add 2 and subtract 2 from the current $\_GET[page] if the get page is greater than 3, but I need to take into account testing to see if the results go that high. So only if results are greater than 10 should number 2 show, and then if on page 16 pages 17 and 18 should only show if there are 170 and 180 results to display. 

Perhaps I have just been staring at the code to long, but I can't seem to get a formula or loop correct in my head to even attempt the code page.