Problems with current tab and page numbers in php

混江龙づ霸主 提交于 2019-12-06 13:40:22

After struggling with this long piece of code the only thing that i see that can be causing an issue is $listname

if listname is not a letter your code will not work.

here is what i suggested, since your code is not sorted alphabetically you should consider using numbers instead. A-Z will be 1 to 10 or 1 to total pages it makes much more sense,

here is a class that i use to handle pagination easily

    class Pagination {

            public $current_page;
            public $per_page;
            public $page_count;

            public function __construct($page=1,$per_page= 15, $total_count=0) {
                    $this->current_page = $page;
                    $this->per_page = $per_page;
                    $this->total_count = $total_count;
            }

            public function offset() {
                    return ($this->current_page -1)* $this->per_page;
            }

            public function total_pages () {
                    return ceil($this->total_count/ $this->per_page);
            }

            public function previous_page () {
                    return $this->current_page -1;
            }

            public function next_page () {
                    return $this->current_page +1;
            }

            public function has_previous_page () {
                    return $this->previous_page() >= 1? true : false;
            }

            public function has_next_page () {
                    return $this->next_page() <= $this->total_pages()? true : false;
            }

// edit this section to suit your needs
            public function format_output ($link,$query) {
                    if ($this->total_pages() > 1) {
                            $output = "<div id=\"pagination\"><p>";
                            $output .= ($this->has_previous_page())? ("<a href=\"/$link/".$this->previous_page()."/{$query}\" >Previous</a>"): "";
                            $c = $this->current_page;
                            $t = $this->total_pages();
                            for ( $i = 1; $i <= $t; $i++ )
                            {
                                    if ($this->current_page == $i) {
                                            $output .= " <a class=\"active\" href=\"#\" >{$i}</a> ";
                                    }else {
                                            $output .= " <a href=\"/$link/{$i}/{$query}\" >{$i}</a> ";
                                            //$output .= " <a href=\"$link?q={$query}&page={$i}\" >{$i}</a> ";
                                    }
                            }
                            $output .= (($t ==$this->total_pages())?"":' ... ');
                            $output .= ($this->has_next_page())? ("<a href=\"/$link/".$this->next_page()."/{$query}\" >Next</a>"): "";
                            $output .= "</p></div>";

                    }else {
                            $output = "<div id=\"pagination\"><p>Displaying all results.</p></div>";
                    }
                    return $output;
            }
    }

    ?>

and here is how i use it.

// your listname in numbers
$currentPage = isset($_GET['currentPage'])? $_GET['currentPage']: 1; // page number 
$limit =2; // you decided to limit 2 per page
$total = 10; // the total result available in all pages

$pagination = new Pagination($currentPage, $limit,$total);

// if you need the the value of how many to offset for the current page
$offset = $pagination->offset(); // example page 6 will return 12

// and to display the pagination simply echo this
echo $pagination->format_output(htmlentities($_SERVER['PHP_SELF']),'other values u want to pass');

This will automatically create the previous and next button for you

I hope this will help you solve your problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!