php - pagination

Discussion in 'PHP' started by Jeremy Benson, Oct 15, 2014.

  1. #1
    Hey,

    I'm just wondering if someone can help me wrap my head around this pagination. I know this is a leaning tower, but it's almost working, lol. How come my prepages work, but pages that come after current page aren't limiting right.

    $items = array('jeremy', 'bob', 'sam', 'ben');
    
    $current_page = $_GET['page'];
    $per_page = 5;
    $total = 500;
    $totalPages = ceil($total/$per_page);
    
    echo 'Current: ' . $current_page . '<br />';
    echo 'Total: ' . $totalPages . '<br /> <br />';
    
    
    foreach($items as $val)
    {
    
      echo $val . '<br/>';
    
    }
    
    echo '<br/>';
    
    echo '<< ';
    
    for($i = $current_page; $i <= $totalPages; $i++)
    {
    
    if($current_page == 1)
    {
    if($i == $current_page)
    {
     
       echo '<b>' . $i . '</b>' . ' ';
    }else{
       echo $i . ' ';
    }
    if($i == 5)
    {
      break;
    }
    
    
    }else{
    
      if($current_page - 5 >= 1)
      {
     
       echo $i - 5 . ' ';
       echo $i - 4 . ' ';
       echo $i - 3 . ' ';
       echo $i - 2 . ' ';
       echo $i - 1 . ' ';
       
       
        if($i == $current_page)
        {
     
          echo '<b>' . $i . '</b>' . ' ';
        }else{
       
       
           echo $i . ' ';
       
        }
       
         if($i > $current_page + 4)
         {
        
        
          break;
        
         }
    
       
     
      }else{
     
        
         switch($current_page)
         {
        
          
           case 2:
            echo $i - 1 . ' ';
           break;
           case 3:
            echo $i - 2 . ' ';
            echo $i - 1 . ' ';
           break;
           case 4:
            echo $i - 3 . ' ';
            echo $i - 2 . ' ';
            echo $i - 1 . ' ';
           break;
           case 5:
           echo $i - 4 . ' ';
           echo $i - 3 . ' ';
           echo $i - 2 . ' ';
           echo $i - 1 . ' ';
           break;
         }
          
         if($i == $current_page)
         {
     
          echo '<b>' . $i . '</b>' . ' ';
        }else{
       
          echo $i . ' ';
       
        }
    
         if($i > $current_page + 4)
         {
        
           break;
        
         }
     
      }
    
    }
    
    }
    
    echo '>>';
    
    PHP:
     
    Solved! View solution.
    Jeremy Benson, Oct 15, 2014 IP
  2. #2
    You made this really complicated I think. What are you trying to do? You have 100 total pages but your script only seems to allow for 5?

    Would this be what you want?

    
    $items = array('jeremy', 'bob', 'sam', 'ben');
    
    $current_page = $_GET['page'];
    $per_page = 5;
    $total = 500;
    $totalPages = ceil($total/$per_page);
    echo 'Current: ' . $current_page . '<br />';
    echo 'Total: ' . $totalPages . '<br /> <br />';
    foreach($items as $val)
    {
      echo $val . '<br/>';
    }
    echo '<br/>';
    echo '<< ';
    for($i = ($current_page - 5); $i <= ($current_page + 5); $i++)
    {
        if ($i > 0 && $i <= $totalPages) {
            if ($i == $current_page) {
                echo ' <b>'.$i.'</b> ';
            } else {
                echo ' '.$i.' ';
            }
        }
    }
    echo '>>';
    
    PHP:
    This should give you a span of 5 pages before the current page and 5 pages after as long as those pages exist.
     
    Anveto, Oct 15, 2014 IP
  3. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #3
    That's perfect. I do tend to over complicate code. I'm just trying to get some overhead to implement with pagination using bootstrap. This should be a near perfect starting point. Thanks.
     
    Jeremy Benson, Oct 15, 2014 IP
  4. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #4
    Hey,

    I just put this code into a pagination class for bootstrap after calculating everything you need to pass in the URL with the last variable in the query string being page. I think it works perfect, but the nav bar being passed back isn't complete. after moving some extra code in I got a bug, lol.

    When checking page source I see only the last bit of the bar gets rendered, so a few conditionals are being missed or something..

    Just wondering if someone can take a look :)

      function return_html($urlSet)
       {
          $html = '';
         
         if($this->current_page == 1)
         {
        
            $html = $html . '<nav style="clear:both;">
                   <ul class="pagination">
                    <li><a><span aria-hidden="true">&laquo;</span><span class="sr-only">Previous</span></a></li>';
        
         }else{
         
            $html = $html . '<nav style="clear:both;">
                   <ul class="pagination">
                    <li><a href="'.$urlSet. $this->current_page - 1 .'"><span aria-hidden="true">&laquo;</span><span class="sr-only">Previous</span></a></li>';
         } 
          
         for($i = ($this->current_page - 5); $i <= ($this->current_page + 5); $i++)
          {
            if ($i > 0 && $i <= $this->totalPages) {
               if ($i == $this->current_page) {
                  $html = $html . '<li><a href="'.$urlSet.$i.'" style="background-color:lightblue;"><b>'.$i.'</b></a></li>';
               } else {
                  $html = $html . '<li><a href="'.$urlSet.$i.'">'.$i.'</a></li>';
               }
             }
          }
         
          if($this->current_page != $this->totalPages)
          {
            
             $html = $html . '<li><a href="'.$urlSet. $this->current_page + 1 .'"><span aria-hidden="true">&raquo;</span><span class="sr-only">Next</span></a></li>
                            </ul>
                          </nav>';
         
          }else{
         
             $html = $html . '<li><a><span aria-hidden="true">&raquo;</span><span class="sr-only">Next</span></a></li>
                            </ul>
                          </nav>';
         
          }
         
          return $html;
       }
    
    PHP:
     
    Jeremy Benson, Nov 14, 2014 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Not sure why you're self-buffering the output, that's usually a waste of memory ESPECIALLY if you're already gzip buffering... even if you were to do that you're wasting time saying "$html = $html . " when you could just be saying "$html .= "

    Of course you've got the pointless HTML 5 code bloat and silly aria role nonsense too; much less the span for nothing, static style in the markup, and outright redundant/pointless use of IF on identical outputs slowing execution and being just more code bloat.

    Here, try mine:

    function pageList($urlRoot, $currentPage, $perPage, $totalArticles) {
    
      if ($totalArticles > $perPage) {
    
        $urlRoot .= '?page=';
    
        echo '
          <div class="pageList">
            Pages:
            <ul>';
    
        $lastPage = floor(($totalArticles - 1) / $perPage);
    
        if ($lastPage > 0) {
          echo '
              <li><a href="', $urlRoot, '0">First</a></li>';
          if ($currentPage>0) echo '
              <li><a href="', $urlRoot, $currentPage - 1, '">&laquo;</a></li>';
        }
    
        if ($lastPage > 9) {
          $counter = ($currentpage < 6) ? 0 : $currentPage - 5;
          $endPage = $counter + 10;
          if ($endPage > $lastPage) $endPage = $lastPage;
        } else {
          $counter = 0;
          $endPage = $lastPage;
        }
    
        while ($counter <= $endPage) {
          $nonAnchor = ($counter == $currentPage);
          echo '
              <li>', (
                $nonAnchor ? '<span>' : '<a href="' . $urlRoot . $counter . '">'
              ), ++$counter, (
                $nonAnchor ? '</span>' : '</a>'
              ), '</li>';
        }
    
        if ($lastPage > 0) {
          if ($currentPage < $lastPage) echo '
              <li><a href="', $urlRoot, $currentPage + 1, '">&raquo;</a></li>';
          echo '
              <li><a href="', $urlRoot, $lastPage, '">Last</a></li>
            </ul>
          <!-- .pageList --></div>';
        }
    
      }
    
    } // function pageList()
    Code (markup):
    Should be verbose enough for you to figure out -- if not go ahead and ask.

    Seriously though, if you have to put that much markup into a variable, and "have" to use that many elements for something as simple as pagination, you're doing something wrong.
     
    Last edited: Nov 15, 2014
    deathshadow, Nov 15, 2014 IP
  6. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #6
    Tried this, but got some bad output. Not sure why.

     <ul>
              <li><a href="account.php?option=vidoes&page=0">First</a></li>
              <li><a href="account.php?option=vidoes&page=0">&laquo;</a></li>
              <li><a href="account.php?option=vidoes&page=0">1</a></li>
              <li><span>2</span></li>
              <li><a href="account.php?option=vidoes&page=2">3</a></li>
              <li><a href="account.php?option=vidoes&page=2">&raquo;</a></li>
              <li><a href="account.php?option=vidoes&page=2">Last</a></li>
            </ul>
    HTML:
    Set up

    $pager->set_currentpage($currentPage);
      $pager->count_items("select count(*) from videos");
      $pager->set_totalpages();
      $pager->calculate_offset();
    PHP:
    function call

    
      echo $pager->pageList('account.php?option=vidoes&', $currentPage, 5, $pager->return_totalitems());
    
    PHP:
     
    Jeremy Benson, Nov 16, 2014 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #7
    Actually, that output would be CORRECT -- if you are on the second page (index 1) the first page is zero, the previous page is zero, and page zero is zero. Likewise if you are on page 2 of 3, index 2 would be the last, next and 3rd page.

    ... at least assuming $currentPage is 1; my routine was set up to run from a zero index, is that your problem? For example if you had 3 pages they'd be numbered 0..2 not 1..3

    For example, if you had five pages and was on page 3 (index 2)

            <ul>
              <li><a href="account.php?option=vidoes&page=0">First</a></li>
              <li><a href="account.php?option=vidoes&page=1">&laquo;</a></li>
              <li><a href="account.php?option=vidoes&page=0">1</a></li>
              <li><a href="account.php?option=vidoes&page=1">2</a></li>
              <li><span>3</span></li>
              <li><a href="account.php?option=vidoes&page=3">4</a></li>
              <li><a href="account.php?option=vidoes&page=4">5</a></li>
              <li><a href="account.php?option=vidoes&page=3">&raquo;</a></li>
              <li><a href="account.php?option=vidoes&page=4">Last</a></li>
            </ul>
    Code (markup):
    Would be the correct output.
     
    deathshadow, Nov 16, 2014 IP
  8. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #8
    I wonder why I'm getting errors... When I click the link for 3rd page it sets my url page variable to 2, but I get errors in my foreach loop for showing displays saying invalid argument, but this variable is defined in the main code, not some logical control that doesn't get reached. If I load the page from the main link to get there so everything will be reset my videos show and the url is set... if I change the page value in the url itself to 2 the page will load, but there's no videos.... If I click the link that takes me back to the page to reset everything page goes back to 1.. then I click link 3 in the pagination it sets the url page variable to 2 but I get errors and no output.... Not sure what's going on... there are 3 pages worth of videos in the database... three links in the pagination code generated.... but the values to page through pagination links is: one is 0 page 2 is 1 and page 3 is 2.... I'm not sure why the errors are coming up, or why I would want my pagination links like that... instead of page 1 setting page variable to 1 and so on... lol
     
    Jeremy Benson, Nov 17, 2014 IP
  9. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #9
    Okay, I've got the first problem figured out. The base url I fed in had a misspelled variable... I don't get errors anymore.. still... there's definitely a bug somewhere.... The pagination output is first (page=0) 1 (page=0) 2 (page=1) 3 (page=2) last (page=2) Now if I click first or 1 to set page to 0 I get no output... clicking page 2 will show first results and set page to 1....clicking on 3 sets page to 2... but there's a third page that never gets shown... clicking on final sets the page to 2 same as 3.... but I think if page 1 set page to 1 and so on up until 3 everything would have shown... I don't understand page 0 and not showing last page...
     
    Jeremy Benson, Nov 17, 2014 IP
  10. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #10
    Just up the variables by one in the code (so you don't use 0, but instead 1).
     
    PoPSiCLe, Nov 17, 2014 IP
  11. Mark_KW

    Mark_KW Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #11
    I wouldn't say I know much. But I think this related to the video I saw on YouTube last night
     
    Mark_KW, Dec 10, 2014 IP