Read, With the Name of Your Lord Who Created

Page Number Generator

Posted by triaslama on May 13, 2008

One of the way to prevent show all of data in one page is pagination. Pagination is the process organizing information in pages. For instance, the information appear in a web pages is paginated such that appear 10 lines for every page.
Pagination can be a cool way to manage the looks and feels of information. Because too many tabular data often tedious, hard, or we maybe easily miss something if all of information dropped in a page.

So, what is needed to do pagination? I think one of the most apparent is a list of page! I have write a program (a simple program) in C# that generates page number. The input parameters is current page, number of rows in a page, and the amount of rows.

Output of this progam is array of integer, every number reflected page number being showed. And this is the listing of program:

using System;

namespace PageNumbersGenerator
{
 public class ListOfPage
{
    public ListOfPage()
    {
    }

    private const int pListLength = 5;
    private int totalPages;
    private int startPosition, endPosition;

    private int[] pages;

    public int[] GetPages(int currentPage, int rowsPerPage, int totalRows)
    {
        double tempTotRows = totalRows;
        double tempRowsPerPage = rowsPerPage;
        double tempTotPages = Math.Ceiling((tempTotRows/rowsPerPage));
        this.totalPages = (int) tempTotPages;

        if (currentPage < 1)
            currentPage = 1;
        if (currentPage > this.totalPages)
            currentPage = this.totalPages;

        if (totalPages >= pListLength)
        {
            this.pages = new int[pListLength];

            startPosition = currentPage-2;
            endPosition = currentPage+2;
            if (startPosition<=0)
            startPosition = 1;

            int nShift = 0;
            if (endPosition>this.totalPages)
            {
                nShift = endPosition - this.totalPages;
            }
            startPosition = startPosition - nShift;
            // fill the pages

            for (int i=0;i<pages.Length;i++)
            {
                 pages[i] = startPosition;
                 startPosition++;
            }
        }
         else
        {
             this.pages = new int[totalPages];

             // fill the pages number
             for (int k=0;k<pages.Length;k++)
             {
                 pages[k] = k+1;
            }
        }

        return pages;
    }
}
}

The first thing I done to create a list of page is compute the total pages. We get the total pages by divide the total rows data with rows data per page.

double tempTotRows = totalRows;
double tempRowsPerPage = rowsPerPage;
double tempTotPages = Math.Ceiling((tempTotRows/rowsPerPage));
this.totalPages = (int) tempTotPages;

I use two temporary variables each of them as double data type because Math.Ceiling need a double or decimal parameter passed to it. Then I check if total pages greater or equal to five the pages variable has 5 as its length, otherwise it has total pages as its length.

We also need to to make sure that the list of page doesn’t out of range of available position (that is startPosition must greater or equal to 1 and endPosition must smaller or equal to total pages).

if (startPosition<=0)
   startPosition = 1;
if (startPosition>this.totalPages)
{
    nShift = endPosition - this.totalPages;
}
startPosition = startPosition - nShift;

We can compile the program as a library and refer it. To test the above code make a simple class that use the ListOfPage class. The following is an example:

class TestApp
{
	static void Main()
	{
		PageNumbersGenerator.ListOfPage pn = new PageNumbersGenerator.ListOfPage();

		int[] pages = pn.GetPages(8,10,123);
		foreach (int page in pages)
		{
			Console.Write(" "+page);
		}
		Console.WriteLine();
	}
}

In TestApp class I specify 8, 10, and 123 for start page (current page), number of data in one page, and total data respectively. The listing above will produce the following output:
6 7 8 9 10

Try to modify the first parameter I hope you will get more sense on the page number generator class. I think the above codes will just a mock up if we not really implement it to solve a problem. I plan it for my next post, so just wait!

4 Responses to “Page Number Generator”

  1. […] Page Number Generator […]

  2. aziz said

    Bukannya masalah ini sudah selesai waktu buat lapis?

  3. mytcbaoej yodsiea ucro gybu hctjzf hmgqaedk gjoym

  4. Interesting article: Will visit soon=D

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: