SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Pagination

  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Manchester, UK
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pagination

    I've written a CF pagination script that currently displays links for every single page that is required. The limit of items per page is 3, so when the database grows with articles it's obviously gonna have a gazillion page buttons.

    I'm partially stuck on the logic I need for the next step... having a maximum of 6 page buttons, which, depending on the current page will display lower pages on the left and higher pages on the right, separated with "..." for example.

    This is the code I've written up to now for testing:

    Code:
    <!--- Limit per page --->
    <cfset limit = 3>
    
    <!--- Start row (from URL) --->
    <cfparam name="url.start" type="numeric" default="1">
    <cfif NOT isDefined("url.start")>
    	<cfset url.start = 1>
    </cfif>
    
    <!--- Get data from start row to end row --->
    <cfquery name="data" datasource="#Application.dsn#">
    	SELECT COUNT(*) AS count
        FROM news
    </cfquery>
    
    <!--- Calculate pages --->
    <cfset pages = int(data.count / limit)>
    
    <cfset separator = int(pages / 2)>
    
    <div class="pages">
        Pages:
        <!--- For every 3 rows print page button --->
        <cfloop index="page" from="1" to="#pages#">
            
        	<!--- Calculate the newStart value --->
        	<cfset newStart = (1 + ((page - 1) * 3))>
            
            <!--- Output button with newStart value --->
            <!--- Active button --->
            <cfif Ceiling(start / limit) EQ page>
            <cfoutput>
                <a href="/news/?start=#newStart#" class="active">#page#</a>
            </cfoutput>
            <cfelse>
            <cfoutput>
                <a href="/news/?start=#newStart#">#page#</a>
            </cfoutput>
            </cfif>
            
        </cfloop>
    </div>
    Basically - 139 rows in a table, 3 rows per page, 6 page buttons per page with the first 3 lower values and the last 3 higher values, e.g.
    Code:
    [1] [2] [3] ... [44] [45] [46]
    However say if you were on page 28, it'd need to display
    Code:
    [25] [26] [27] ... [29] [30] [31]
    for example.

    Any advice or help would be appreciated!

  2. #2
    SitePoint Zealot YAHA's Avatar
    Join Date
    Jan 2008
    Location
    MD, US
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are trying to get the math and logic for displaying 3 preceding and 3 following pages, given a currently selected page, one of the ways is this. First of all, let me mention that you can transfer the number of the current page through an URL variable (I assume thats what you use the "start" variable for). Then, the actual script should contain a code that looks at the variable passed through URL and then makes some calculations to display proper page links. Say the current page number is contained in "current". Then the logic should work like this :

    Code:
       if "Current" is greater than "Number of Pages Preceding to Display"(in your case this is equal 3) 
       than 
         //// display links to pages preceding
    
       for i from "Number of Pages Preceding to Display" down to 1 
             display link to the page "Current" - i
    
      else
      //// this is the case when your current page selected is closer to the beginning of the count than Number of Pages Preceding to Display (your case 3)
       for i from Current - 1 down to 1
          display link to the page "Current" - i
    
    This completes the logic for displaying the preceding pages.
    
    if "Total Count" -"Current" is greater than "Number of Pages Following to Display"(in your case this is equal 3) 
       than 
         //// display links to pages following
    
       for i from 1 to "Number of Pages Following to Display" 
             display link to the page "Current" + i
    
      else
      //// this is the case when your current page selected is closer to the end of the count than Number of Pages Following to Display (your case 3)
       for i from 1 to (("Total Count" - "Current") - 1)
          display link to the page "Current" + i


    Sorry that I didnt provide the actual CF code. Let me know if you have questions or need clarifications. Also, I may write an article about this on my blog. If I do, I will shoot you a link.


    P.S. Also, note that a good style of programming requires generalities. In other words, try not to make hard references to numbers in your loops and calculations. Given the above logic, you should be able to change the "Number of Pages Preceding" and "Number of Pages Following" to automatically change the number of links displayed before and after the selected page.

  3. #3
    SitePoint Zealot YAHA's Avatar
    Join Date
    Jan 2008
    Location
    MD, US
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Say there are 10 pages total.

    case 1 - you are on page 4
    [1] [2] [3] ... [5] [6] [7]

    case 2 - you are on page 3
    [1] [2] ... [4] [5] [6]

    case 3 - you are on page 8
    [5] [6] [7] ... [9] [10]


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
  •