More simplified way of creating page links dynamically? A little birdie suggested I bring this question here, so here it goes.

Well I have a working script (see below) but it seems quite clunky and redundant; in my defense I wrote this code many moons ago, but that's not the point. I was curious if anyone has an idea on a more efficient way of writing this code, with less loops and conditionals and, well, noise in the code.

Code in question:

private function pageLinks($num, $page = 1, $search = false, $ne = false) {
    $query = ($search) ? '&amp;query='.$search : null;
    $by = (is_numeric($ne)) ? '&amp;by='.$ne : null;
    $links = 'Page(s):&lt;a href="search.php?page=1' . $query . $by . '" class="tableLink"&gt;1&lt;/a&gt;';
    $count = 1;
    $npp = $this-&gt;numPerPage;
    $buttons = 9;
    $half = 4;
    for($i = 1; $i &lt;= $num; $i++) {
        if(($i%$npp) === 0) {
            $count++;
        }
    }
    if($count &lt; $buttons) {
        for($i = 2; $i &lt;= $count; $i++) {
            $links .= '&lt;a href="search.php?page=' . $i . $query . $by . '" class="tableLink"&gt;' . $i . '&lt;/a&gt;';
        }
    } elseif($page &lt;= ($half + 2)) {
        for($i = 2; $i &lt;= $buttons; $i++) {
            $links .= '&lt;a href="search.php?page=' . $i . $query . $by . '" class="tableLink"&gt;' . $i . '&lt;/a&gt;';
        }
        $links .= '...&lt;a href="search.php?page=' . $count . $query . $by . '" class="tableLink"&gt;' . $count . '&lt;/a&gt;';
    } elseif($page &lt;= ($count - ($half + 2))) {
        $links .= '...';
        for($i = $half; $i &gt; 0; $i--) {
            $links .= '&lt;a href="search.php?page=' . ($page - $i) . $query . $by . '" class="tableLink"&gt;' . ($page - $i) . '&lt;/a&gt;';
        }
        $links .= '&lt;a href="search.php?page=' . ($page - $i) . $query . $by . '" class="tableLink"&gt;' . ($page - $i) . '&lt;/a&gt;';
        for($i = 1; $i &lt;= $half; $i++) {
            $links .= '&lt;a href="search.php?page=' . ($page + $i) . $query . $by . '" class="tableLink"&gt;' . ($page + $i) . '&lt;/a&gt;';
        }
        $links .= '...&lt;a href="search.php?page=' . $count . $query . $by . '" class="tableLink"&gt;' . $count . '&lt;/a&gt;';
    } else {
        $links .= '...';
        for($i = $buttons - 1; $i &gt;= 0; $i--) {
            $links .= '&lt;a href="search.php?page=' . ($count - $i) . $query . $by . '" class="tableLink"&gt;' . ($count - $i) . '&lt;/a&gt;';
        }
    }
    return($links);
}


The method is called like so:

$links = $this-&gt;pageLinks($count, $page, $url, $ne);


And the variables are as such:

$count = total number of clients in database (int)
$page = current page to build from (int)
$url = the name or email for the search (String)
$ne = is for the search string either by name (1) or email (2) (int)

And the output is something like (as links):

Page(s):1 2 3 4 5 6 7 8 9...33

Or if you're in the middle (page 20):

Page(s):1...16 17 18 19 20 21 22 23 24...33

Now this isn't always called through a search function, hence the default values for $url and $ne, but that's not very important. My question is there a cleaner way to handle building of these links? Or am I stuck with this cluster of loops?
This:

$count = 1;
for($i = 1; $i &lt;= $num; $i++) {
    if(($i%$npp) === 0) {
        $count++;
    }
}


is equivalent to

$count = floor($num / $npp) + 1;


As for the paging, I'd do it like this:

$from = $page - $half;
if ($from &lt;= 2) $from = 2;
$to = $page + $half;
if ($to &gt;= $count - 1) $to = $count - 1;

$extra = $query . $by;
$links = $this-&gt;pageLink(1, $extra);
if ($from &gt; 2) $links .= "...";
for ($i = $from; $i &lt;= $to; $i++)
    $links .= $this-&gt;pageLink($i, $extra);
if ($i &lt; $count) $links .= "..."; // I use $i instead of $to because $i == $to + 1, so I save one addition
$links = $this-&gt;pageLink($count, $extra);


Here, you also need:

private function pageLink($num, $extra) {
    return '&lt;a href="search.php?page=' . $num . $extra . '" class="tableLink"&gt;' . $num . '&lt;/a&gt;';
}


This was written by heart, so be weary of the possible bugs.