$_GET['offset'] - Page number links

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • printline
    New Member
    • Jul 2006
    • 89

    $_GET['offset'] - Page number links

    Hello

    I have a problem which i hope someone can help me with. I have a website where customers can login and their current and previous orders.

    What i need now is for the customers to look through their orders. I kind of already have a script for this:

    [code=php]$recordcount = mssql_num_rows( $result);
    $pagecount = $recordcount / $displayPerPage ;
    $r = fmod($recordcou nt, $displayPerPage );
    if ($r > 0) {
    $pagecount = floor($pagecoun t) + 1;
    }
    if ($pagecount < 1) { $pagecount = 1; }
    if (mssql_num_rows ($result) > 0) {
    if (isset($_GET['offset']) && $_GET['offset'] != 0 ) {
    mssql_data_seek ($result, $_GET['offset']);
    $rowsLeft -= $_GET['offset'];
    $currentPage = ($recordcount - $rowsLeft) / $displayPerPage ;
    $r = fmod(($recordco unt - $rowsLeft), $displayPerPage );
    if ($r > 0) {
    $currentPage = floor($currentP age) + 1;
    }

    }[/code]

    I also have a script that shows the customer how many orders that were found and on how many pages the orders are shown at. ex. 67 orders found, page 1 of 7.

    [HTML]<?php print $recordcount ?> orders found, page <?php print $currentPage ?> of <?php print $pagecount ?>[/HTML]

    I also have a next and previous button script:

    [HTML]<?php
    if (isset($_GET['offset']) && $_GET['offset'] != 0 ) {
    echo "<a href=\"?offset= " . ($_GET['offset'] - $displayPerPage ) . "\">" . "Previous</a>";
    }
    ?>

    <?php
    if ($rowsLeft > 0) {
    echo "<a href=\"?offset= " . (@$_GET['offset'] + $displayPerPage ) . "\">" . "Next</a>";
    }
    ?>[/HTML]

    What i need is numbers with links in between the previous and next buttons that the customer can click to go that exact page of the order view. Ex.

    1 2 3 4 5 6 7 Next

    Can anyone help me making these number links.......... ..?

    Thanks in advance...
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    You mean something like this?

    [code=php]
    <?php
    // Get the number of pages
    $pageCount = 10;

    // Get the current page
    $currentPage = @$_GET['currentPage'];
    if(!@isset($_GE T['currentPage'])) {
    $currentPage = 0;
    }
    else if($currentPage >= $pageCount) {
    $currentPage = $pageCount - 1;
    }

    // Print the Prev link if needed
    if($currentPage > 0) {
    echo '<a href="?currentP age='. ($currentPage - 1) .'">Prev</a> ';
    }

    // Print the number list
    for($i = 0; $i < $pageCount; $i++) {
    echo '<a href="?currentP age='. $i .'">'. ($i + 1) .'</a> ';
    }

    // Print the Next link if needed
    if($currentPage < $pageCount - 1) {
    echo '<a href="?currentP age='. ($currentPage + 1) .'">Next</a>';
    }
    ?>[/code]
    Last edited by Atli; Aug 17 '07, 12:36 AM. Reason: Fixed a syntax error in the code

    Comment

    • anonymous
      Banned
      New Member
      • Sep 2005
      • 99

      #3
      i done this before and i got it working, i was proud.

      heres the formula -

      on index page, you count(*) the quatity from database and then divide by how many display per page then so if quatity = 50 and display per page is 10

      there would be 5 pages -> previous [1] 2 3 4 5 next

      each links would be echo by

      [code=php]
      $pages = 50/10; //which is 5
      $i = 0;
      while($i <= $pages) { echo "<a href="./index.php?page= $i">"; i++ }[/code]

      and then when the previous and next links are click, it is retrive as below
      [code=php]
      <?php
      $db_select_star t = $_REQUEST[page]*10;

      // run database query
      select * from data where id = $db_select_star t;

      while($row)
      { etc.. you know this and then echo below}
      ?>[/code]

      notice all codes are not sure to be correct, im just showing my formula, but it may not be correct after all because i did not make sure.
      Last edited by pbmods; Aug 12 '07, 12:03 AM. Reason: Added CODE tags.

      Comment

      • printline
        New Member
        • Jul 2006
        • 89

        #4
        To Atli

        I tried putting in your code and it printed out the page numbers. But regardless which number i click on it only takes me one page forward (seems that the number links react like the next button).

        Also if i have more than 10 pages it gives me a problem. Solved this by putting in this code:

        [PHP]<?php
        // Get the number of pages
        $pageCount = $recordcount / $displayPerPage ;
        // Print the number list
        for($i = 0; $i < $pageCount; $i++) {
        echo "<a href=\"?offset= " . (@$_GET['offset'] + $displayPerPage ) . "\">". ($i + 1) .'</a> ';
        }

        ?>[/PHP]

        But then i get all the page numbers displayed. I would like there to be only 10 numbers visible per page. 1-10 on first page and if i hit the next button once, it should be 2-11 and so on.....

        Can you help with that.......?
        Last edited by Atli; Aug 17 '07, 12:00 AM. Reason: Switched code tags to PHP

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by printline
          To Atli

          I tried putting in your code and it printed out the page numbers. But regardless which number i click on it only takes me one page forward (seems that the number links react like the next button).
          Yea sorry, a minor syntax error in my code at line 21. It should look like this.
          - I've changed this in my original post, btw
          [code=php]
          echo '<a href="?currentP age='. $i. '">'. ($i + 1) .'</a> ';
          [/code]
          Originally posted by printline
          Also if i have more than 10 pages it gives me a problem. Solved this by putting in this code:

          [PHP]<?php
          // Get the number of pages
          $pageCount = $recordcount / $displayPerPage ;
          // Print the number list
          for($i = 0; $i < $pageCount; $i++) {
          echo "<a href=\"?offset= " . (@$_GET['offset'] + $displayPerPage ) . "\">". ($i + 1) .'</a> ';
          }

          ?>[/PHP]

          But then i get all the page numbers displayed. I would like there to be only 10 numbers visible per page. 1-10 on first page and if i hit the next button once, it should be 2-11 and so on.....

          Can you help with that.......?
          What you need to do is calculate the first and the last pages that you want to display, based on the current page.
          Also, note that pages 1 through 10 are not 10 numbers, they are only 9. Four on each side of the current page.

          So, that could be done like this:
          [code=php]
          // Get the first and last index shown
          $startIndex = 0;
          $endIndex = $pageCount - 1;
          if($pageCount > 10) {
          // Calculate start and end index
          $startIndex = $currentPage - 4;
          $endIndex = $currentPage + 4;

          // Check if values are out of bounds and adjust
          if ($startIndex < 0) {
          $endIndex -= $startIndex;
          $startIndex = 0;
          }
          if($endIndex > $pageCount - 1) {
          $startIndex += ($pageCount - $endIndex);
          $endIndex = $pageCount - 1;
          }
          }
          [/code]

          And then you will have to modify your for loop to use those values. Like so:
          [code=php]
          for($i = $startIndex; $i <= $endIndext; $i++) {
          [/code]

          Comment

          • sharathy
            New Member
            • Jul 2007
            • 2

            #6
            [code=php]
            <?php
            $que = "select COUNT(clm_topic Id) AS numrows from tbl_blogtopics" ;
            $resu = mysql_query($qu e) or die('Error, query failed');
            $ro = mysql_fetch_arr ay($resu, MYSQL_ASSOC);
            $numrows = $ro['numrows'];
            $maxPage = ceil($numrows/$rowsPerPage);
            $self = $_SERVER['PHP_SELF'];
            if ($pageNum > 1)
            {
            $page = $pageNum - 1;
            $prev = " <a href=\"$self?pa ge=$page\">[Prev]</a> ";

            $first = " <a href=\"$self?pa ge=1\">[First Page]</a> ";
            }
            else
            {
            $prev = ' [Prev] ';
            $first = ' [First Page] ';
            }
            if ($pageNum < $maxPage)
            {
            $page = $pageNum + 1;
            $next = " <a href=\"$self?pa ge=$page\">[Next]</a> ";

            $last = " <a href=\"$self?pa ge=$maxPage\">[Last Page]</a> ";
            }
            else
            {
            $next = ' [Next] ';
            $last = ' [Last Page] ';
            }
            echo "<br>";
            echo $first.$prev."< strong style='color:#F FFFFF'>Showing& nbsp;page&nbsp; $pageNum&nbsp;o f&nbsp;$maxPage &nbsp;pages&nbs p;</strong>".$next. $last;
            }

            [/code]


            try with this
            sharathy
            Last edited by Atli; Aug 17 '07, 12:28 PM. Reason: Added code tags.

            Comment

            • printline
              New Member
              • Jul 2006
              • 89

              #7
              To Atli

              I am nearly there. I can see in my url that something happens. Fx. my first page looks like this in the url: http://portal.printlin e.dk/search.php and then if i hit the next button or the page number 2 it says: http://portal.printlin e.dk/search.php?curr entPage=1
              If i hit the number 4 link for example it says: http://portal.printlin e.dk/search.php?curr entPage=3

              The problem is that it is still the same page that is showing (same 10 orders). It is only the url that changes.

              Here is all the code i am using:

              [PHP]<?php
              // Now write the data lines
              $recordcount = mssql_num_rows( $result);
              $pagecount = $recordcount / $displayPerPage ;
              $r = fmod($recordcou nt, $displayPerPage );
              if ($r > 0) {
              $pagecount = floor($pagecoun t) + 1;
              }
              if ($pagecount < 1) { $pagecount = 1; }
              if (mssql_num_rows ($result) > 0) {
              if (isset($_GET['offset']) && $_GET['offset'] != 0 ) {
              mssql_data_seek ($result, $_GET['offset']);
              $rowsLeft -= $_GET['offset'];
              $currentPage = ($recordcount - $rowsLeft) / $displayPerPage ;
              $r = fmod(($recordco unt - $rowsLeft), $displayPerPage );
              if ($r > 0) {
              $currentPage = floor($currentP age) + 1;
              }

              }
              $i = 0;
              if ($currentPage < 1) { $currentPage = 1; }

              while ($jtsstatus = mssql_fetch_arr ay($result)) {
              if ($i >= $displayPerPage ) {
              break;
              }
              $i += 1;
              // Get the first and last index shown
              $startIndex = 0;
              $endIndex = $pageCount - 1;
              if($pageCount > 10) {
              // Calculate start and end index
              $startIndex = $currentPage - 4;
              $endIndex = $currentPage + 4;

              // Check if values are out of bounds and adjust
              if ($startIndex < 0) {
              $endIndex -= $startIndex;
              $startIndex = 0;
              }
              if($endIndex > $pageCount - 1) {
              $startIndex += ($pageCount - $endIndex);
              $endIndex = $pageCount - 1;
              }
              }

              ?>[/PHP]

              And

              [PHP]<?php
              // Get the number of pages
              $pageCount = 10;

              // Get the current page
              $currentPage = @$_GET['currentPage'];
              if(!@isset($_GE T['currentPage'])) {
              $currentPage = 0;
              }
              else if($currentPage >= $pageCount) {
              $currentPage = $pageCount - 1;
              }

              // Print the Prev link if needed
              if($currentPage > 0) {
              echo '<a href="?currentP age='. ($currentPage - 1) .'">Prev</a> ';
              }

              // Print the number list
              for($i = 0; $i < $pageCount; $i++) {
              echo '<a href="?currentP age='. $i .'">'. ($i + 1) .'</a> ';
              }

              // Print the Next link if needed
              if($currentPage < $pageCount - 1) {
              echo '<a href="?currentP age='. ($currentPage + 1) .'">Next</a>';
              }
              ?>[/PHP]

              Can you see what is wrong......???? ??????

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Yea, the second code snippet is still using the preset $pageCount that I used in my example (@ line 3), which is ten. You need to change this into the $pagecount that you calculated in your first snippet.

                Ps. You can replace " = floor($value) + 1" with " = ceil($value)" and it will give you the same result.

                Comment

                • printline
                  New Member
                  • Jul 2006
                  • 89

                  #9
                  I changed what you suggested, but then i get all the page number links. For example if i have 65 pages with orders it shows me the numbers from 1 - 65 and not as intended only 1 - 10.....?

                  Comment

                  Working...