Highlighted

Pagination

Community Beginner ,
Jan 31, 2018

Copy link to clipboard

Copied

Hi. I have a press release page and I would like to put a page pagination on it at the bottom, so there aren't so many results on one page. I would like it to have a previous and next link and numbers the user can click on. I found some code online that almost works, but when I click on the different page numbers or next for example, only the result numbers display correctly, but the actual results coming from my database do not update or change. You can see my example here: http://www.ironwoodelectronics.com/press/pagination_pages/pages.cfm I got the code from this web site: https://coldfusioncenter.wordpress.com/2014/09/22/easy-pagination-with-mysql-and-coldfusion/

    I still have some formatting to work on with the links. I was going to incorporate it into the Bootstrap pagination numbers at the very bottom. But I just wanted to test out and see how to do a pagination first and see if I could get it to work. The top 5 sections are what would show up when the user first clicks on the Press Release link. I have this set up as a library item in Dreamweaver so I can update this with the newest info, but if I get this pagination to work correctly, I think I won't need to use the library item and I can just pull the info. from the database.

     Please look towards the bottom where the numbered list with the months and years are at. This is the pagination code I created that is coming from the SQL database.  At first, the results show up perfect with Jan. 2018 being at the top since that's the newest. But if you click the next link it goes from 6-10 which is good, but the months and years don't change at all. Does anyone know why this is?

    I was also having problems with limiting to only showing 5 results at a time because I couldn't get this line of code to work about limiting:

LIMIT #start_record#, #records_per_page#  I was able to get this to work by putting maxrows="5" into the cfquery field like this:

<cfquery name="get_names" datasource="#application.DataSource#" result="get_data" maxrows="5">

SELECT names

FROM pages_names

ORDER BY id DESC

</cfquery>

    I'm not sure if this is the correct way or not though. This is also where the Limit line was in the example above that I couldn't get to work. We are running SQL now too. I think I've read this is a SQL line of code.

    I've added my code below and a screenshot of what I have in the database. Does anyone know how to get this to work with the correct info. displaying on each page and limiting to only having 5 records display at a time? Thanks.

Inline image

<cfparam name="url.page" default="1">

<cfset page_links_shown = 5>

<cfset records_per_page = 5>

<cfset start_record = url.page * records_per_page - records_per_page>

<cfquery name="get_count" datasource="#application.DataSource#">

SELECT COUNT(id) AS records

FROM pages_names

</cfquery>

<cfquery name="get_names" datasource="#application.DataSource#" result="get_data" maxrows="5">

SELECT names

FROM pages_names

ORDER BY id DESC

</cfquery>

<cfset total_pages = ceiling(get_count.records / records_per_page)>

<cfoutput>

<cfloop query="get_names">

#start_record + currentrow#. #names#<br/>

</cfloop>

<hr>

<cfif url.page EQ 1>

Prev Page

<cfelse>

<a href="pages.cfm?page=#url.page-1#">Prev Page</a>

</cfif>

<hr>

<cfif url.page * records_per_page LT get_count.records>

<a href="pages.cfm?page=#url.page+1#">Next Page</a>

<cfelse>

Next Page

</cfif>

<hr>

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

<cfparam name="show_pages" default="#min(page_links_shown,total_pages)#">

<cfif url.page + int(show_pages / 2) - 1 GTE total_pages>

<cfset start_page = total_pages - show_pages + 1>

<cfelseif url.page + 1 GT show_pages>

<cfset start_page = url.page - int(show_pages / 2)>

</cfif>

<cfset end_page = start_page + show_pages - 1>

<cfloop from="#start_page#" to="#end_page#" index="i">

<cfif url.page EQ i>

#i#

<cfelse>

<a href="pages.cfm?page=#i#">#i#</a>

</cfif>

</cfloop>

<hr>

<a href="pages.cfm?page=1">First Page</a>

<hr>

<a href="pages.cfm?page=#total_pages#">Last Page</a>

<hr>

</cfoutput>

Andy

Views

276

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Pagination

Community Beginner ,
Jan 31, 2018

Copy link to clipboard

Copied

Hi. I have a press release page and I would like to put a page pagination on it at the bottom, so there aren't so many results on one page. I would like it to have a previous and next link and numbers the user can click on. I found some code online that almost works, but when I click on the different page numbers or next for example, only the result numbers display correctly, but the actual results coming from my database do not update or change. You can see my example here: http://www.ironwoodelectronics.com/press/pagination_pages/pages.cfm I got the code from this web site: https://coldfusioncenter.wordpress.com/2014/09/22/easy-pagination-with-mysql-and-coldfusion/

    I still have some formatting to work on with the links. I was going to incorporate it into the Bootstrap pagination numbers at the very bottom. But I just wanted to test out and see how to do a pagination first and see if I could get it to work. The top 5 sections are what would show up when the user first clicks on the Press Release link. I have this set up as a library item in Dreamweaver so I can update this with the newest info, but if I get this pagination to work correctly, I think I won't need to use the library item and I can just pull the info. from the database.

     Please look towards the bottom where the numbered list with the months and years are at. This is the pagination code I created that is coming from the SQL database.  At first, the results show up perfect with Jan. 2018 being at the top since that's the newest. But if you click the next link it goes from 6-10 which is good, but the months and years don't change at all. Does anyone know why this is?

    I was also having problems with limiting to only showing 5 results at a time because I couldn't get this line of code to work about limiting:

LIMIT #start_record#, #records_per_page#  I was able to get this to work by putting maxrows="5" into the cfquery field like this:

<cfquery name="get_names" datasource="#application.DataSource#" result="get_data" maxrows="5">

SELECT names

FROM pages_names

ORDER BY id DESC

</cfquery>

    I'm not sure if this is the correct way or not though. This is also where the Limit line was in the example above that I couldn't get to work. We are running SQL now too. I think I've read this is a SQL line of code.

    I've added my code below and a screenshot of what I have in the database. Does anyone know how to get this to work with the correct info. displaying on each page and limiting to only having 5 records display at a time? Thanks.

Inline image

<cfparam name="url.page" default="1">

<cfset page_links_shown = 5>

<cfset records_per_page = 5>

<cfset start_record = url.page * records_per_page - records_per_page>

<cfquery name="get_count" datasource="#application.DataSource#">

SELECT COUNT(id) AS records

FROM pages_names

</cfquery>

<cfquery name="get_names" datasource="#application.DataSource#" result="get_data" maxrows="5">

SELECT names

FROM pages_names

ORDER BY id DESC

</cfquery>

<cfset total_pages = ceiling(get_count.records / records_per_page)>

<cfoutput>

<cfloop query="get_names">

#start_record + currentrow#. #names#<br/>

</cfloop>

<hr>

<cfif url.page EQ 1>

Prev Page

<cfelse>

<a href="pages.cfm?page=#url.page-1#">Prev Page</a>

</cfif>

<hr>

<cfif url.page * records_per_page LT get_count.records>

<a href="pages.cfm?page=#url.page+1#">Next Page</a>

<cfelse>

Next Page

</cfif>

<hr>

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

<cfparam name="show_pages" default="#min(page_links_shown,total_pages)#">

<cfif url.page + int(show_pages / 2) - 1 GTE total_pages>

<cfset start_page = total_pages - show_pages + 1>

<cfelseif url.page + 1 GT show_pages>

<cfset start_page = url.page - int(show_pages / 2)>

</cfif>

<cfset end_page = start_page + show_pages - 1>

<cfloop from="#start_page#" to="#end_page#" index="i">

<cfif url.page EQ i>

#i#

<cfelse>

<a href="pages.cfm?page=#i#">#i#</a>

</cfif>

</cfloop>

<hr>

<a href="pages.cfm?page=1">First Page</a>

<hr>

<a href="pages.cfm?page=#total_pages#">Last Page</a>

<hr>

</cfoutput>

Andy

Views

277

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 31, 2018 0

Have something to add?

Join the conversation