SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple Pagination with modulus

    I am in the process of trying to learn the concept of modulus and I have been told that I can use it in the process of pagination. Anyone care to explain?

    I have a very simple array and I want to put 2 results per page. (this oviously is just to get the process in my head)

    PHP Code:
    <?php

    error_reporting
    (E_ALL);

    $animals = array("cat""dog""rat""rabbit""lion""tiger""mouse""monkey""fish""fox");

    foreach (
    $animals as $animals_num => $animal) {

    //what to do here?

    };

    ?>

  2. #2
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    modulus is the 'number left over' when b is divided by a

    try this little code snippet to see what I mean

    PHP Code:
     $num_per_page 3;
     
    $max_items 10;
     for (
    $i 0$i $max_items$i++)
     {
       echo 
    $i .' mod ' $num_per_page ' = ' $i $num_per_page '<br />'
     } 
    $num_per_page is the number of items you want on a page.

  3. #3
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll give this a try and worry about displaying page links later.

  4. #4
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand how I can show only 3 animals per page with this?

  5. #5
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    error_reporting
    (E_ALL);

    $animals = array("cat""dog""rat""rabbit""lion""tiger""mouse""monkey""fish""fox");

    $num_per_page 3;

    $p $_GET['p'];

    for (
    $i 0$i count($animals); $i++) {

        if (
    $i $num_per_page == $p) {
        
        echo 
    $animals[$i];
        
        };
        
    };

    ?>
    I am getting some useful output now I think.

  6. #6
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another problem is that they won't be in key order as the array is...? This becomes a problem because I want to later apply this to a array which is in order..

  7. #7
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok - here is a complete example that

    1) displays the reqested data
    2) builds the links

    PHP Code:
      $animals = array("cat""dog""rat""rabbit""lion""tiger""mouse""monkey""fish""fox");
      
    $num_per_page 3;
      
    $max_items 10;
      
    $total_items count($animals);
      
      
    // read passed in page number and validate it
      
    if (true === isset($_GET['page']))
      {
       
    $start intval($_GET['page']);
       if ( (
    $start 0) || ($total_items $start) )
       {
        
    $start 0;
       }
      }
      else
      {
       
    $start 0;
      }
      
      
    // calcualte start and end ponits in array
      
    $start *= $num_per_page;
      
    $end $start $num_per_page;
      if (
    $total_items $end)
      {
        
    $end $total_items;
      }
      
      
    // Display the data
      
    for ($i $start$i $end$i++)
      {
        echo 
    'Animal at offset ' $i ' is ' $animals[$i] . '<br />';
      }
      
      
    // build links
      
    $link_start '<a href="' $_SERVER['PHP_SELF'] . '?page=';
      
    $link_end '</a>';
      
      
    $num_pages ceil($total_items $num_per_page);
      for (
    $i 0$i $num_pages$i++)
      {
       echo 
    $link_start $i '">Page ' $i $link_end '&nbsp;';
      } 
    Hopefully there are enough comments for you to modify as required.
    Also, I have tried to make it so that it checks for an invalid page number being passed.

  8. #8
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    $animals 
    = array("cat""dog""rat""rabbit""lion""tiger""mouse""monkey""fish""fox");

    $num_per_page 3;

    $p $_GET['p'];

    if ( empty(
    $p) || $p ) {

        
    $p 0;
        
    };

    for (
    $i $p $num_per_page$i < ($p+1) * $num_per_page$i++) {

        echo 
    $animals[$i] . "<br />";
        


    echo 
    '<a href="' $_SERVER['PHP_SELF'] . '?p=' . ($p+1) . '">next page</a>'

    ?>

  9. #9
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code does work, however

    1) Once you have viewed a page, there is no way to go back to it, or any previous page

    2) You can view past the number of items in the $animals array - not recommended

  10. #10
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll muck around with it later. Thanks for your help. My head hurts. ~

  11. #11
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you please explain your code indepth so I can learn something from it?

  12. #12
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    error_reporting
    (E_ALL);

    // put file contents into array
    $contents array_reverse(file('quotedb'));

    // number of data entries to be displayed
    $num_per_page 20;

    // total entries
    $total_items count($contents);

    //
    if (isset($_GET['p'])) {
        
    $start intval($_GET['p']);
        if ((
    $start 0) || ($total_items $start)) {
            
    $start 0;
        }
    } else {
        
    $start 0;
    }

    // calcualte start and end ponits in array
    $start *= $num_per_page;
    $end $start $num_per_page;

    if (
    $total_items $end) {
        
    $end $total_items;
    }

    // display the data
    for ($i $start$i $end$i++) {
        
    $line $contents[$i];
        
    $explode explode('#0'$line);
        
    $date explode('-'$explode[3]);
        echo 
    '<b>' $explode[0] . '</b><br />' $explode[1] . '<br/><i>added by ' $explode[2] . ' on the ' date('l \t\h\e jS \o\f F, Y'mktime(0,0,0,$date[1],$date[0],$date[2])) . '</i><br /><br />';
    }

    // build links
    $num_pages ceil($total_items $num_per_page);

    for (
    $i 0$i $num_pages$i++) {
        echo 
    '<a href="' $_SERVER['PHP_SELF'] . '?page=' $i '">Page ' $i '</a>&nbsp;|&nbsp;';
    }

    ?>
    My edited code.

  13. #13
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Certainly - here goes

    PHP Code:
       $animals = array("cat""dog""rat""rabbit""lion""tiger""mouse""monkey""fish""fox"); 
    This is the complete list of items (things) that you want to display, page by page

    PHP Code:
      $num_per_page 3;
      
    $max_items 10;
      
    $total_items count($animals); 
    oops, remove the $max_items variable - it is not required.
    $num_per_page - this is the number of items to be displayed on a single page.
    $total_items - this is the total number of items to be displayed.

    PHP Code:
       // read passed in page number and validate it
        
    if (true === isset($_GET['page']))
        {
         
    $start intval($_GET['page']);
         if ( (
    $start 0) || ($total_items $start) )
         {
          
    $start 0;
         }
        }
        else
        {
         
    $start 0;
        } 
    Ths section of code is used to retrieve the page number from the link ($_GET['page']) which is then stored in $start.
    $start is then checked to make sure that is is between the first and last item in the array. Why? becase you don't want to display non-existent items.

    PHP Code:
        // calcualte start and end ponits in array
        
    $start *= $num_per_page;
        
    $end $start $num_per_page;
        if (
    $total_items $end)
        {
          
    $end $total_items;
        } 
    This section of code calculates the start position in the array i.e the first item that you want to display on the selcted page, and also the last item to be displayed, so that you don;t display non-existent items.

    PHP Code:
        // Display the data
        
    for ($i $start$i $end$i++)
        {
          echo 
    'Animal at offset ' $i ' is ' $animals[$i] . '<br />';
        } 
    This section of code actually displays the data, 1 items per line.

    PHP Code:
        // build links
        
    $link_start '<a href="' $_SERVER['PHP_SELF'] . '?page=';
        
    $link_end '</a>'
    This section of code just creates some variables to be used when generating the links

    PHP Code:
      $num_pages ceil($total_items $num_per_page); 
    Calculate the total number of pages to generate links for. ceil returns the next highest valus of the passed in variable, roundng up where necessary

    PHP Code:
        for ($i 0$i $num_pages$i++)
        {
         echo 
    $link_start $i '">Page ' $i $link_end '&nbsp;';
        } 
    This section of codes generates and displays the page links

    So
    Page 0 displays animals 0 to 2
    Page 1 displays animals 3 to 5
    Page 2 displays animals 6 to 8
    Page 3 displays animals 9 to 10

    I hope this explains things in more detail.

    Run this code and then do a 'Vie Source' to see what the links look like.

    Don't hesitate to ask more questions if I have glossed over anything.

  14. #14
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is great, the main thing I didn't get was the start and finish variables but I get it now! Thanks mate. Code is working @ http://www.passivekid.com/~nathan/quotebot/quotedb.php

  15. #15
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problems.

    Glad it is now working.

    One thing, rather than have pages go from 0 to 6, I think it looks better if they go from 1 to 7.

    This is easily achieved by modding this one little bit of code

    Original code
    PHP Code:
     for ($i 0$i $num_pages$i++)
     {
      echo 
    $link_start $i '">Page ' $i $link_end '&nbsp;';
     } 
    New code
    PHP Code:
     for ($i 0$i $num_pages$i++)
     {
      echo 
    $link_start $i '">Page ' . ($i 1) . $link_end '&nbsp;';
     } 
    Last edited by swdev; Jan 4, 2005 at 07:16. Reason: Added suggestion

  16. #16
    SitePoint Enthusiast nathanj's Avatar
    Join Date
    Dec 2004
    Location
    Perth, WA, Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hehe yeah. Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •