Custom Search

Thursday, January 24, 2008

Tutorial : Pagination (Function based)

hi,
this is second part of pagination example.here i show pagination technique
that is reusable.at first i create a function for pagination then call it when need.
lets do the job.

create a page name pagination.cfm,code bellow.

pagination.cfm
 <cfoutput>#Pagination("Suman","admin")#</cfoutput>
 
 <cffunction name="Pagination" access="remote" returntype="any" output="yes">
  <cfargument name="dsname" required="yes">
  <cfargument name="TableName" required="yes">
  <cfargument name="MaxRow" default="10" required="no">
  
  <cfparam name="url.PageNumber" default="1">
  
  <cfif NOT IsDefined("session.qSelectQuery") OR IsDefined("url.CreateNewSession")>
   <cfquery name="session.qSelectQuery" datasource="#dsname#">
    SELECT *
    FROM #TableName#
   </cfquery>
  </cfif>
  
  <cftable query="session.qSelectQuery" startrow="#((url.PageNumber*MaxRow)-MaxRow)+1#" maxrows="#MaxRow#" colheaders="yes">
   <cfloop list="#session.qSelectQuery.ColumnList#" index="Column">
  <cfcol text="#Evaluate(Column)#" header="#Column#">
   </cfloop>
  </cftable>
  
  <cfoutput>
   <cfloop from="1" to="#Ceiling(session.qSelectQuery.RecordCount/MaxRow)#" index="i">
  <a href="#CGI.SCRIPT_NAME#?PageNumber=#i#">#i#</a>
   </cfloop>
   .........page #url.PageNumber# of #Ceiling(session.qSelectQuery.RecordCount/MaxRow)#......
   <a href="#CGI.SCRIPT_NAME#?CreateNewSession">run the select query again</a>
  </cfoutput>
 </cffunction>
   
the pagination function is done.you can use it in your page more than one
time without write huge code....