First I am very new to ColdFusion, but am learning pretty quickly. So I am trying to build a large database that originally displays all results with 25 lines per page and have a next/prev link to navigate through the pages.
That all works fine, but when I perform a search, and when the new results display of about a couple of pages worth, the pagination links don't work. When I click on the "next" link it goes back to the original all records display. How can I fix this or what do I need to do to make it work?
Sorry I'm new at 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 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 see more...thank you.
Here is an example I whipped up (sorry if it is terse), and it covers things you already discussed with Mark. I also like Mark's <cfloop>
examples above (below). Lol...Where ever this response ends up.
So we have:
- query recordcount (max)
- starting in your range
- ending in your range
- output per page
With bonus pageNum querystring for your next grouping of records (which I think is something you would like).
Then it can look like this in 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>
You need to show how you are actually navigating in your code - that's where the secret sauce is buried. You have everything else you need (maybe more than you need).
You probably have a cfoutput or cfloop in your code somewhere. You would use your startrow and displayrows to output a set number of rows from the records - like so:
<Cfoutput query="records" startrow="#next#" maxrows="#displayrows#">
... code to output your data goes here
</cfoutput>
If you are using cfloop it is similar.
<Cfloop query="records" startrow="#next#" endrow="#next+displayrows#">
...code to output your data.
</cfloop>
You can also use an index loop like so:
<cfloop from="#next#" to="#next+displayrows#" index="x">
.... your outputs will look like this:
#records[columname][x]#
</cfoutput>
HOpefully one of those samples will ring a bell. The logic you put in your code snippets is only creating a starting point and defining how many loops. It's the output that teases out the records to display.
Also note the comment - you almost never need evaluate()
in your code.
I worked this out using a cfform tag with BACK - MORE - HOME submit buttons.
The first page had the query for ID 1 to 25 and a MORE submit button.
hidden field was the count 25
The next page had HOME and MORE buttons
Home had a hidden field of 1
More had a hidden field of count + 25 (50)
The next page had BACK HOME and MORE buttons
Back had hidden field of count - 25
HOME had a hidden field of 1
MORE had a hidden field of count + 25 (75)
and so on.
Query used the number of the hidden field depending on the value of the SUBMIT button to create the query WHERE and output the 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 query
SELECT *
FROM mytabl
WHERE ID BETWEEN #count1# AND #count2#
The display
<table>
<cfoutput query="myquery">
<tr>
<td>
#my data1#
</td>
<td>
#my data2#
</td>
</cfoutput>
</tr>
<table>