Pagination displaying the wrong way round

Hey awesome php people!

We’ve got a pagination script working, but it outputs backwards (highest to lowest number). Can someone help?

<?php   
if ($total > $pagesize)
{
    if ($currentPage != $previous) 
    {
?>
            <a href="<?php echo $_SERVER["PHP_SELF"] ."?pagesize=$pagesize&page=$previous"; ?>">&laquo; Previous</a>
<?php
    }
    
    $pages = $paging["pages"];
    for($i = count($pages) - 1; $i >= 0; $i--) 
    {
        $pageNumber = $pages[$i];
?>
        <?php if ($pageNumber == $currentPage) { ?>
        <span class="current"><?php echo $pageNumber ?></span>
        <? } else { ?>
        <a href="<?php echo $_SERVER["PHP_SELF"] . "?pagesize=$pagesize&page=$pageNumber" ?>"><?php echo $pageNumber ?></a>
        <? } ?>
<?php
    }
    
    if ($currentPage != $next)
    { 
?>
            <a href="<?php echo $_SERVER["PHP_SELF"] . "?pagesize=$pagesize&page=$next"; ?>">Next &raquo;</a>
<?php
    }
} 
?>

MJ & Starlion,

You rule!! Works like a charm, thank you! :slight_smile:

PS: If you’re going to paginate, generally you dont have a page 0.

It appears you are counting down from the total amount of pages in your “for” loop:

You are using: (this counts down)
for($i = count($pages) - 1; $i >= 0; $i–)

Try changing it to: (this counts up)
for($i = 0;$i<count($pages);$i++)

(note: I have not tried it - just gave it a quick scan)

Let me know if it makes a difference.

MJ