Coldfusion and pagination

First, I'm very new to ColdFusion, but I'm pretty quick to learn. So I am trying to create a large database that initially outputs all results with 25 rows per page and has a next / previous link to navigate through the pages.

Everything works fine, but when I do a search and when the new results are displayed for about a couple of pages, the page links don't work. When I click on the "next" link, it goes back to the original screen of all entries. How can I fix this or what do I need to do to make it work?

Sorry I'm new to posting and this is my first one. Hope this is better.

My pagination code ...

<cfset Next = StartRow + DisplayRows>
<cfset Previous = StartRow - DisplayRows> 

<cfoutput> 
  <cfif Previous GTE 1> 
    <a href="#CGI.Script_Name#?StartRow=#Previous#"><b>Previous #DisplayRows# Records</b></a> 
  <cfelse> 
    Previous Records 
  </cfif> 
  <b> | </b> 
  <cfif Next lte records.RecordCount> 
    <a href="#CGI.Script_Name#?StartRow=#Next#"><b>Next 
    <cfif (records.RecordCount - Next) lt DisplayRows> 
      #Evalute((records.RecordCount - Next)+1)# 
    <cfelse> 
      #DisplayRows# 
    </cfif>Records</b></a>
  <cfelse> Next Records 
  </cfif> 
<cfoutput>

      

My code is at the top ...

<cfparam name="StartRow" default="1"> 
<cfparam name="DisplayRows" default="25"> 

<cfset ToRow = StartRow + (DisplayRows - 1)> 
<cfif ToRow gt records.RecordCount> 
  <cfset ToRow = records.RecordCount> 
</cfif>

      

Let me know if you need to know more ... thanks.

+3


source to share


3 answers


Here's an example I whipped up (sorry if it's short) and it covers things you've already discussed with Mark. I also like Mark's examples <cfloop>

above (see below). Lol ... Wherever this answer is.

So we have:

  • query recordcount (max)
  • starting from your range
  • ends in your range
  • output to page


With the bonus of pageNum querystring for your next grouping of records (which I think you need).

Then it might look like this on your page:

<cfparam name="pageNum" default="1">

<cfquery name="q" datasource="#application.dsn#">
    select * from yourTable 
</cfquery>

<cfset maxRows = 10>
<cfset startRow = min( ( pageNum-1 ) * maxRows+1, max( q.recordCount,1 ) )>
<cfset endRow = min( startRow + maxRows-1, q.recordCount )>
<cfset totalPages = ceiling( q.recordCount/maxRows )>

<cfset loopercount = round( q.recordCount/10 )>

<cfoutput>
    <cfloop from="1" to="#looperCount#" index="i">
            <a href="?pageNum=#i#">#i#</a>
    </cfloop>
</cfoutput> 

<br><br>

<cfoutput
    query="q" 
    startrow="#startRow#"
    maxrows="#maxRows#">

    #id#<br>

</cfoutput>

      

0


source


You need to show how you actually float in your code - where the secret sauce is hiding. You have everything you need (maybe more than you need).

You probably have cfoutput or cfloop code somewhere. You must use start and display lines to output a given number of lines from records - for example:

<Cfoutput query="records" startrow="#next#" maxrows="#displayrows#">

... code to output your data goes here

</cfoutput>

      

If you are using cfloop it looks like.

<Cfloop query="records" startrow="#next#" endrow="#next+displayrows#">

...code to output your data. 

</cfloop>

      



You can also use an index loop like this:

<cfloop from="#next#" to="#next+displayrows#" index="x">

.... your outputs will look like this: 
#records[columname][x]#

</cfoutput>

      

Wish one of these samples would ring the bell. The logic that you put in your code snippets only creates a starting point and determines how many loops. This is the output that teases the displayed entries.

Also note the comment - you almost never need it evaluate()

in your code.

0


source


I worked with this cfform tag with BACK - MORE - HOME return buttons.

On the first page, I requested an ID from 1 to 25 and a Submit button. the hidden field was 25

The next page had the HOME and MORE buttons Home had a hidden field 1 More had a hidden field count + 25 (50)

The next page had buttons BACK HOME and MORE Back has a hidden count field - 25 HOME had a hidden field 1 MORE had a hidden count field + 25 (75)

etc.

The query used the number of the hidden field depending on the value of the SUBMIT button to create a WHERE query and display 25 rows

 <cfif submit IS "NEXT">

   <cfset count1 = #count# + 1>
   <cfset count2 = #count# + 25>

 <cfelseif submit is "BACK">

   <cfset count1 = #count# - 26>
   <cfset count2 = #count#>

 <cfelseif submit is "HOME">

   <cfset count1 = 1>
   <cfset count2 = 25>

 </cfif>

      

In request

 SELECT *
 FROM mytabl
 WHERE ID BETWEEN #count1# AND #count2#

      

Display

 <table>
   <cfoutput query="myquery">
     <tr>
       <td>
         #my data1#
       </td>
       <td>
         #my data2#
       </td>
    </cfoutput>
     </tr>
  <table>

      

-1


source







All Articles