Pagination in reverse order

2019-08-11 04:00发布

问题:

So I'm playing around with this PHP tutorial and I kinda got stuck. I have a piece of code for pagination that right now displays pages in ascending order, and I'd like it to display them in descending order, but I can't figure out how to do it. Can someone please help me out?

            if($pagination->total_pages() > 1) {

        for($i=1; $i <= $pagination->total_pages(); $i++){
            if($i == $page){
                echo " <span class=\"selected_page\">{$i}</span> "; 
            }else{
                echo " <a href=\"index.php?page={$i}\">{$i}</a> ";  
            }
        }
    }

回答1:

You can turn the for-loop around to iterate down, like so:

for($i = $pagination->total_pages(); $i >= 1; $i--)

This way $i will start at the latest page, and iterate down until 1.