I have to do pagination in my project:
below code working fine just I'm not able to figure out for (next / prev) buttons how can i handle. im using mysql database it will be great help if you give me some best practice tips & if there is any other easy way? please give me reference it will be great help
here is my code:
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="application/javascript"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!---
<cfquery name="data" datasource="#application.dsn#">
select * from yourTable
</cfquery>
--->
<!--- Generate fake data --->
<cfset data = queryNew("id,name","integer,varchar")>
<cfloop index="x" from="1" to="80">
<cfset queryAddRow(data, {id:x,name:"User #x#"})>
</cfloop>
<cfparam name="URL.PageId" default="0">
<cfset RecordsPerPage = 5>
<cfset TotalPages = (data.Recordcount/RecordsPerPage)-1>
<cfset StartRow = (URL.PageId*RecordsPerPage)+1>
<cfset EndRow = StartRow+RecordsPerPage-1>
<cfoutput>
<table border="1">
<tr>
<th>No.</th>
<th>PARKNAME</th>
<th>REGION </th>
<th>STATE</th>
</tr>
<cfloop query="data">
<cfif CurrentRow gte StartRow >
<tr>
<td>#CurrentRow#</td>
<td>#name#</td>
<td>US</td>
<td>MO</td>
</tr>
</cfif>
<cfif CurrentRow eq EndRow>
<cfbreak>
</cfif>
</cfloop>
</cfoutput>
<tr>
<td colspan="4">
<cfloop index="Pages" from="0" to="#TotalPages#">
<cfoutput>
<cfset DisplayPgNo = Pages+1>
<cfif URL.PageId eq pages>
<strong>#DisplayPgNo#</strong>
<cfelse>
<a href="?PageId=#Pages#">#DisplayPgNo#</a>
</cfif>
</cfoutput>
</cfloop>
</td>
</tr>
</table>
<nav aria-label="...">
<ul class="pagination">
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1">Previous</a>
</li>
<cfloop index="Pages" from="0" to="#TotalPages#">
<cfoutput>
<cfset DisplayPgNo = Pages+1>
<cfif URL.PageId eq pages>
<li class="page-item">
<li class="page-item active">
<a class="page-link" href="##">#DisplayPgNo#</a>
</li>
<cfelse>
<li class="page-item"><a class="page-link" href="?PageId=#Pages#">#DisplayPgNo#</a></li>
</cfif>
</cfoutput>
</cfloop>
<li class="page-item">
<a class="page-link" href="#">Next</a>
</li>
</ul>
</nav>
Here is a full pagination solution. Though you'd probably want to put the paginateBS
function in a reusable application variable.
<!--- CURRENT PAGE --->
<cfparam name="url.page" default="1">
<cfparam name="request.PAGI_pagenum" default="#url.page#">
<!--- RECORDS PER PAGE (ON ALL PAGES) --->
<cfparam name="request.PAGI_allperpage" default="20">
<!--- RECORDS PER PAGE (ON FIRST PAGE) --->
<cfparam name="request.PAGI_firstperpage" default="#request.PAGI_allperpage#">
<!--- NUMBER OF PAGE LINKS AT A TIME --->
<cfparam name="request.PAGI_blocksof" default="5">
<cfset fromRecord = request.PAGI_firstperpage+(request.PAGI_allperpage*(request.PAGI_pagenum-1))-request.PAGI_allperpage>
<cfquery name="quotes" >
SELECT SQL_CALC_FOUND_ROWS first_name, last_name, email, id, datetime, published
FROM quotes
ORDER by datetime DESC
LIMIT #fromRecord#,
<cfif request.PAGI_pagenum is 1>
#request.PAGI_firstperpage#
<cfelse>
#request.PAGI_allperpage#
</cfif>
</cfquery>
<Cfquery name="result_count" >
SELECT FOUND_ROWS() as totalRecords
</cfquery>
<div class="pull-right" id="page">
<div class="pagination pagination-custom">
<ul>
<cfoutput>#paginateQuotes(totalRecords=result_count.totalRecords)#</cfoutput>
</ul>
</div>
</div>
<cffunction name="paginateBS">
<cfargument name="totalRecords" default="0">
<cfparam name="request.PAGI_attribute" default="">
<!--- 'TOP' IS THE LAST PAGE NUMBER LINK --->
<Cfset top = request.PAGI_blocksof * ceiling((request.PAGI_pagenum)/request.PAGI_blocksof)>
<cfoutput>
<!--- IF WE'RE PAST THE FIRST BLOCK OF PAGES, SHOW THE PREV LINK --->
<cfif request.PAGI_pagenum gt request.PAGI_blocksof>
<li>
<a href="?page=#top-request.PAGI_blocksof#<cfif request.PAGI_attribute neq ''>&#request.PAGI_attribute#=#request.PAGI_attributevalue#</cfif>">
<i class="icon-double-angle-left"></i>
</a>
</li>
</cfif>
<!--- LOOP THROUGH THE PAGES IN THIS BLOCK --->
<cfloop from="#top-request.PAGI_blocksof+1#" to="#top#" index="a">
<li <cfif request.PAGI_pagenum is not a>class="active"</cfif>>
<a href="?page=#a#<cfif request.PAGI_attribute neq ''>&#request.PAGI_attribute#=#request.PAGI_attributevalue#</cfif>">#a#</a>
</li>
<!--- IF WE'RE ON THE LAST BLOCK AND DON'T HAVE ENOUGH RECORDS TO COMPLETE THE BLOCK,
STOP CREATING THE LINKS FOR MORE PAGES, AND SET A FLAG TO NOT SHOW THE LINK FOR THE NEXT BLOCK --->
<cfif ((a-1) * request.PAGI_allperpage) + request.PAGI_firstperpage gte arguments.totalRecords>
<cfset arguments.noNext=1>
<cfbreak>
</cfif>
</cfloop>
<!--- IF WE RAN OUT OF RECORDS, DO NOT SHOW THE LINK FOR THE NEXT BLOCK OF PAGES --->
<cfif not isdefined("arguments.noNext")>
<li>
<a href="?page=#top+1#<cfif request.PAGI_attribute neq ''>&#request.PAGI_attribute#=#request.PAGI_attributevalue#</cfif>">
<i class="icon-double-angle-right"></i>
</a>
</li>
</cfif>
</cfoutput>
</cffunction>