1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Pagination

Discussion in 'PHP' started by Jeremy Benson, Apr 18, 2017.

  1. #1
    Hello.

    I've beating my head off this pagination script for a couple of hours. Just wondering if anyone can help me make it do something sensible. 20 pages in the bar would great. Right now my first left angle back is broken out of the html, and when on the last page it never outputs back pages. I just get the first, last page buttons and the next page button.

    <?php
    
       class Pagination{
          
           private $url;
           private $page;
           private $total_items;
           private $items_per_page;
           private $total_pages;
           private $limit;
           private $offset;
          
           function __construct($itemCount, $item_per_page, $url, $page)
           {
              
               $this->total_items = $itemCount;
               $this->url = $url;
              
                $this->page = (is_numeric($page)? $page : 1);
                
               $this->items_per_page = $item_per_page;
              
               $this->total_pages = ceil($this->total_items / $this->items_per_page);
                                    
           }
          
           public function set_limit($limit)
           {
              
               $this->limit = $limit;
              
           }
          
           public function set_offset($offset)
           {
              
               $this->offset = $offset;
              
           }
          
           public function html($appended)
           {
              
               $html = '<div><ul class="pagination">
                <li class="page-item"><a class="page-link" href="'.$this->url.'1">&laquo;</a></li>';
               
                if($this->page - 1 != 0)
                {
                   
                    echo '<li class="page-item"><a class="page-link" href="'.$this->url.($this->page - 1).'">&#60;</a></li>';
                   
                }else{
                   
                    echo '<li class="page-item"><a class="page-link" href="'.$this->url.$this->page.'">&#60;</a></li>';
                   
                }
               
               
              
               if($this->total_pages >= 2)
               {
                
                       
                        if($this->page <= 20)
                        {
                           
                            if($this->page != $this->total_pages)
                            {
                                for($i = 1; $i <= $this->total_pages; $i++)
                                            {
                                              
                                               $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.$i.'">'.$i.'</a></li>';
                                              
                                            } 
                       
                            }else{
                               
                                // on the last page, show back to 10 pages
                               
                                for($i = $this->page; $i <= 1; $i--)
                                {
                                          
                                   $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.$i.'">'.$i.'</a></li>';
                                              
                                } 
                               
                               
                            }
                }else
                {
                           
                               
                    for($i = $this->page; $i <= $this->page + 10; $i++)
                    {
                                          
                        $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.$i.'">'.$i.'</a></li>';
                                          
                    } 
                                       
                           
                        }
                       
                   
               }else{
                  
                   $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.$appended.$this->instance.'=1">1</a></li>';
                  
               }
              
                if($this->page + 1 < $this->total_pages)
               {
    
                 $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.($this->page + 1).'">&#62;</a></li>';
          
               }else{
                  
                   $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.$this->page.'">&#62;</a></li>';
                  
               }
              
               $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.$this->total_pages.'">&raquo;</a></li>';
             
               $html .= '</ul></div>';
              
               return $html;
           }
          
           function return_pages()
           {
              
               return $this->total_pages;
              
           }
          
          
    // End pagination class      
    }
    
    
    ?>
    Code (markup):

     
    Jeremy Benson, Apr 18, 2017 IP
  2. LongshotMP

    LongshotMP Peon

    Messages:
    9
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #2
    This may or may not help, but I would take a look here:
    http://www.freezecoders.com/2014/01/simple-pagination-using-php-mysql.html

    This script I have used a few times in the past an integrated into my PHP and works well. It will of course take a lot of work to integrate, but may be better formed.
     
    LongshotMP, Apr 19, 2017 IP
  3. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #3
    Thanks for the reply. Appreciate it, but I'm gonna stick with PDO. Will wait for other replies. Thanks for taking the time, may help another reader :)
     
    Jeremy Benson, Apr 19, 2017 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    Okay, this class is too confusing. You'd be better off using a simple function

    Change this line:
    for($i = $this->page; $i <= 1; $i--)

    change to:
    for($i = $this->page; $i >= 1; $i--)

    Now goto last page and see if you see previous pages link
     
    JEET, Apr 24, 2017 IP
  5. akhileshbc

    akhileshbc Active Member

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    5
    Trophy Points:
    75
    #5
    Maybe going through this CodeIgniter pagination class's code will help you? https://github.com/johnbellone/codeigniter/blob/master/system/libraries/Pagination.php
     
    akhileshbc, Apr 24, 2017 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Normalizing your indentation habits would go a long ways to helping make sense of what you've done.

    Your setters have zero actual checks for range, meaning there is no reason to use setters or to make those variables private.

    If you are declaring a bunch of variables in the same scope, comma delimit instead of saying private every joe-blasted time!

    Are you junk string buffering or not? You're doing echo too -- which is it?

    What's with the DIV for nothing, endless pointless classes for nothing, and so-forth?

    Unless you are outputting this twice per page -- and even THEN, I'm REALLY not sure I'd be doing the string addition approach or even making this an object for that matter. It's pagination, that's not rocket science or some big giant hardcore processing.

    Here, try mine:

    
    function template_pagination($urlRoot, $current, $perPage, $total) {
    
    	if ($total > $perPage) {
    	
    		echo '
    		<div class="pagination">
    			Pages:
    			<ul>';
    		
    		if (($currentPage = floor($current / $perPage)) > 0) echo '
    				<li><a href="', $urlRoot, '0">First</a></li>
    				<li><a href="', $urlRoot, $currentPage - 1, '" title="previous" rel="previous">&laquo;</a></li>';
    		
    		if (($lastPage = floor(($total - 1) / $perPage)) > 9) {
    			$counter = ($currentPage < 6) ? 0 : $currentPage - 5;	
    			if (($endPage = $counter + 10) > $lastPage) {
    				$endPage = $lastPage;
    				$counter = $endPage - 10;
    			}
    		} else {
    			$counter = 0;
    			$endPage = $lastPage;
    		}
    	
    		while ($counter <= $endPage) {
    			echo '
    				<li>', (
    					($noAnchor = ($counter == $currentPage)) ?
    					'<span>' :
    					'<a href="' . $urlRoot . $counter . '">'
    				), ++$counter, (
    					$noAnchor ?
    					'</span>' :
    					'</a>'
    				), '</li>';
    		}
    		
    		if ($currentPage < $lastPage) echo '
    				<li><a href="', $urlRoot, ++$currentPage, '" title="next" rel="next">&raquo;</a></li>
    				<li><a href="', $urlRoot, $lastPage, '">Last</a></li>';
    		
    		echo '
    			</ul>
    		<!-- .pagination --></div>';
    			
    	}
    	
    } // template_pagination
    
    Code (markup):
    ... and for the love of Ghu remember, if your parent wrapper has a perfectly good class or ID on it, and EVERY like child element inside it is getting the same classes, NONE of those child elements should be getting classes!!!

    Oh, and by plugging in rel="next" and rel="prev", the forward/back button in the browser will follow it if there is no history that direction. Handy way to make paginated pages more useful.
     
    deathshadow, Apr 26, 2017 IP