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.

Need to add "Go to last page" in this pager

Discussion in 'PHP' started by qwikad.com, Jan 17, 2016.

  1. #1
    I am trying to add "Go to first page" and "Go to last page" in this code. Obviously "Go to first page" isn't an issue, but I am not sure I know how to make it do "Go to last page".

    
        for($j=$ini;$j<$end;$j++)if(isset($entries[1][$j]))echo str_replace($ovo,$sovim,$entries[1][$j]);
        if($maxp>-1){
            echo '<p>'.strstr($fcontent,'<!--').' <div class="pager"> '; $gap = "";
            for($j=0;$j<$maxp+1;$j++){
                if($j==0||$j==$maxp||($j-$page)*($j-$page)<26){
                    echo $gap; $gap = "";
                    if($j!=$page)echo " <a href=\"?view=page&pagename=updatemain&p=".$j.$si."\"><span class=\"pagerlink\">".($j+1)."</span></a> " ;
                    else echo " <span class=\"current\">".($j+1)."</span> " ;
                }
                else $gap = "...</div>"; 
            }
            echo '</p>';
        }
    }
    
    Code (markup):
     
    qwikad.com, Jan 17, 2016 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I had a wee play with your code because the way you have it laid out it's damn hard to read - you really don't need to be escaping all those quote marks :)

    for($j=$ini; $j<$end; $j++){
        if(array_key_exists($j, $entries[1])){
            echo str_replace($ovo, $sovim, $entries[1][$j]);
        }
        if($maxp > -1){
            echo '<p>'.strstr($fcontent,'<!--').' <div class="pager"> '; 
    
            $gap = "";
            for($j=0; $j<$maxp+1; $j++){
                if($j==0||$j==$maxp||(($j-$page)*($j-$page)<26)) {
                    echo $gap; 
                    $gap = "";
                    if($j!=$page) {
                        echo " <a href='?view=page&pagename=updatemain&p={$j}{$si}'><span class='pagerlink'>".($j+1)."</span></a> " ;
                    }
                    else {
                        echo " <span class='current'>".($j+1)."</span> " ;
                    }
                }
                else {
                    $gap = "...</div>"; 
                }
            }
            echo " <a href='?view=page&pagename=updatemain&p={$maxp}'><span class='pagerlink'>".($maxp)."</span></a> " ;
            echo '</p>';
        }
    }
    Code (markup):
    Not knowing what some of the variables are I was a little in the dark. Is $maxp the length of the article? If not, you might just have to loop through the array to get the maximum number of pages if there's no easy maths.
     
    sarahk, Jan 17, 2016 IP
    NetStar and qwikad.com like this.
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #3
    @sarahk do you mind checking your code again? It goes into a blank page. I am wondering if there's an extra ' or " somewhere or maybe something isn't closed properly.
     
    qwikad.com, Jan 17, 2016 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #4
    Actually I simplified it, seems to be working:

    
        for($j=$ini;$j<$end;$j++)if(isset($entries[1][$j]))echo str_replace($ovo,$sovim,$entries[1][$j]);
        if($maxp>-1){
            echo '<p></p>'.strstr($fcontent,'<!--').' <div class="pager"> '; $gap = "";
    
    echo " <a href=\"?view=page&pagename=updatemain&p=0\"><span class=\"pagerlink\">First</span></a> ";
    
            for($j=0;$j<$maxp+1;$j++){
                if($j==0||$j==$maxp||($j-$page)*($j-$page)<5){
    
    
                    echo $gap; $gap = "";
                    if($j!=$page)echo " <a href=\"?view=page&pagename=updatemain&p=".$j.$si."\"><span class=\"pagerlink\">".($j+1)."</span></a> " ;
                    else echo " <span class=\"current\">".($j+1)."</span> " ;
                }
                else $gap = "...";
            }
            echo "<a href=\"?view=page&pagename=updatemain&p={$maxp}\"><span class=\"pagerlink\">Last</span></a></div>";
        }
    }
    
    Code (markup):
     
    Last edited: Jan 17, 2016
    qwikad.com, Jan 17, 2016 IP
  5. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #5
    And that's the part I didn't know how to get in there. Thanks for showing me how. And the escaping part doesn't bother me. I just needed a quick fix...
     
    qwikad.com, Jan 17, 2016 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    Well... I cleaned it up a bit (you really need to learn the difference between single and double quotes :) )
    
    for ($j = $ini; $j < $end; $j++) {
      if (isset($entries[1][$j])) {
        echo str_replace($ovo,$sovim,$entries[1][$j]);
      }
      if ($maxp > -1) {
        echo '<p></p>'.strstr($fcontent,'<!--').' <div class="pager">';
        $gap = '';
        echo '<a href="?view=page&pagename=updatemain&p=0" class="pagerlink">First</a>';
    
      for ($j = 0; $j < $maxp+1; $j++) {
        if ( $j == 0 || $j == $maxp || ($j - $page)*($j - $page) < 5) {
          echo $gap;
          $gap = '';
          if ($j != $page) {
            echo '<a href="?view=page&pagename=updatemain&p='.$j.$si.'" class="pagerlink">'.($j+1).'</a>';
          } else {
            echo '<span class="current">'.($j+1).'</span>';
          }
        } else {
          $gap = '...</div>';
        }
      }
      echo '<a href="?view=page&pagename=updatemain&p='.{$maxp}.'" class="pagerlink">Last</a>';
      }
    }
    
    PHP:
    Also, mixing using the curly brackets and not using them leaves a mess. The same does the different formatting from almost line to line. Also, why do you have the span inside the <a>-tag? There's no need for it, unless you're doing something weird - just put the class on the a-tag. And... the empty <p></p> before the start of the <div>... what's that for?
     
    PoPSiCLe, Jan 17, 2016 IP
  7. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #7
    Good points, will clean it up later. I just found the code I needed and wanted to get it working first like I wanted it to. Thank you for your code.

    PS it's always hard for me to set a best answer when both people gave me really good solutions. So I'll just leave it at that. Yay! Everyone wins! :)
     
    Last edited: Jan 17, 2016
    qwikad.com, Jan 17, 2016 IP
  8. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #8
    Spacebar must be broken.
     
    NetStar, Jan 17, 2016 IP
    sarahk likes this.