Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 668883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:05:24+00:00 2026-05-14T00:05:24+00:00

I’m trying to create pagination for search results using MySQL and ColdFusion. My intention

  • 0

I’m trying to create pagination for search results using MySQL and ColdFusion. My intention is to only retrieve the queries that can be displayed on a single page, thus making the process efficient. I tried using two queries in my function, but I could not return two variables to the cfinvoke.

The following code does not paginate, but it displays the result search results using a CFC:

<!---DEFINE DEFAULT STATE--->
<cfparam name="variables.searchResponse" default="">
<cfparam name="URL.titleName" default="">
<cfparam name="URL.genreID" default="">
<cfparam name="URL.platformID" default="">

<!---TitleName can only be blank if one or both genre and platform are selected--->
<cfif StructKeyExists(URL, "searchQuery") AND (Len(Trim(URL.titleName)) LTE 2 AND Len(URL.genreID) IS 0 AND Len(URL.platformID) IS 0)>
    <cfset variables.searchResponse = "invalidString">
<cfelseif StructKeyExists(URL, "searchQuery")>
    <cfinvoke component="gz.cfcomp.test" method="searchGames" returnvariable="resultData" argumentcollection="#URL#">
    <cfset variables.searchResponse = "hasResult">
</cfif>

<cfif searchResponse EQ "hasResult" AND resultData.RecordCount EQ 0>
    <cfset variables.searchResponse = "noResult">
</cfif>

Using this logic, I can display what I need to display on the page:

<cfif searchResponse EQ "invalidString">
     <cfoutput>Invalid search</cfoutput>
</cfif>
<cfif searchResponse EQ "noResult">
     <cfoutput>No results found</cfoutput>
</cfif>
<cfif searchResponse EQ "hasResult">
     <cfoutput>Display Results</cfoutput>
</cfif>

If I were executing the queries on the same page, it would be easy to follow the many tutorials out there. But the queries are executing in a function. Displaying the data is easy, but paginating it has become a nightmare for me. Here is my function:

<cffunction name="searchGames" access="public" output="false">
    <cfargument name="titleName" required="no" type="string">
    <cfargument name="genreID" required="no" type="string">
    <cfargument name="platformID" required="no" type="string">    

    <!--- DEFINE LOCAL VARIABLES--->
    <cfset var resultData = "">        
    <!---GET DATA--->
    <cfquery name="resultData" datasource="myDSN">
        SELECT *
            <!---JOINS FOR GENRE/PLATFORM GO HERE--->
        WHERE
            <!---CONDITIONS GO HERE--->
    </cfquery>
    <!---RETURN VARIABLE--->
    <cfreturn resultData>
</cffunction>   

To paginate, I thought about modifying my function to the following (a new query using a count statement):

<!--- DEFINE LOCAL VARIABLES--->
<cfset var resultCount = "">        
<!---GET DATA--->
<cfquery name="resultCount" datasource="myDSN">
    SELECT COUNT(gameID) AS rowsFound FROM GAMES
        <!---JOINS FOR GENRE/PLATFORM GO HERE--->
    WHERE
        <!---CONDITIONS GO HERE--->
</cfquery>
<!---RETURN VARIABLE--->
<cfreturn resultCount>

Then I figured if there is a result to return, I would execute a nested query and create the pagination variables:

<cfif resultCount.rowsFound GTE 0>
<cfparam name="pageNumber" default="1">
<cfset var recordsPerPage = 5>
<cfset var numberOfPages = Int(resultCount.RecordCount / recordsPerPage)>
<cfset var recordsToSkip = pageNumber * recordsPerPage - recordsPerPage>

<!---DEFINE LOCAL VARIABLE--->
<cfset var resultData = "">

<cfquery name="resultData" datasource="myDSN">
<!---GET DATA AND SEND IT BACK USING LIMIT WITH #recordsToSkip# and #RecordsPerPage#--->
</cfquery>
<!---RETURN VARIABLE--->
<cfreturn resultData>
</cffunction>

I figured I would return two variables: resultCount and resultData. I would use #resultCount# to build my pagination, and #resultData# to display the output. The problem is I can’t return two variables in the same cfinvoke tag. Any ideas of how to approach the the right way? I’m totally lost as to the logic I need to follow.


EDIT: I’m using the following code to paginate now (the only problem is now I have to repass all the search filters back into the URL because using #CGI.SCRIPT_NAME# clears them):

<cfif searchResponse EQ "hasResult">
<!---BASICALLY, IF resultCount.rowsFound is not 0, execute this method--->
     <cfinvoke component="gz.cfcomp.test" method="getResult" returnvariable="resultData" argumentcollection="#URL#">

     <cfif URL.currentPage IS 1>
          --
     <cfelse>
          <a href="#CGI.SCRIPT_NAME#?searchQuery=&titleName=#URL.titleName#&genreID=#URL.genreID#&platformID=#URL.platformID#&currentPage=#currentPage-1#">Prev Page</a>
     </cfif>

     <cfif URL.currentPage * recordsPerPage LT resultCount.rowsFound>
          <a href="#CGI.SCRIPT_NAME#?searchQuery=&titleName=#URL.titleName#&genreID=#URL.genreID#&platformID=#URL.platformID#&currentPage=#currentPage+1#">Next Page</a>
     <cfelse>
          --
     </cfif>

</cfif>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:05:25+00:00Added an answer on May 14, 2026 at 12:05 am

    If your results is not huge, you can stay with the same SQL that returns everything and use

    <cfoutput query="data" startrow="#url.start#" maxrows="#recordsPerPage#">
    

    when you display it, see: http://www.coldfusionjedi.com/index.cfm/2006/4/24/ColdFusion-and-Pagination. No Query of Query is needed.

    To answer your question

    The problem is I can’t return two
    variables in the same cfinvoke tag.

    WHY do you want to return two variables in the same cfinvoke? Instead, write 2 functions: countResult() and getResultData(page, RecordsPerPage)

    <cffunction name="countResult" output="false" returntype="numeric">
      <cfset var resultCount = "">    
      <cfquery name="resultCount" datasource="myDSN">
        SELECT COUNT(gameID) AS rowsFound FROM GAMES
            <!---JOINS FOR GENRE/PLATFORM GO HERE--->
        WHERE
            <!---CONDITIONS GO HERE--->
      </cfquery>
      <cfreturn resultCount.rowsFound>
    </cffunction>
    

    For getResultData(page, RecordsPerPage) using true paging in DB level:

    If you want to do true pagnation in DB level, use LIMIT and OFFSET in MySQL.

    <cffunction name="getResultData" output="false" returntype="Query">
      <cfargument name="page" type="numeric" default="1">
      <cfargument name="recordsPerPage" type="numeric" default="5">
    
      <cfset var resultData = "">
      <cfset var offset = (page-1) * RecordsPerPage>
    
      <cfquery name="resultData" datasource="myDSN">
        SELECT * LIMIT #recordsPerPage# OFFSET #offset#
            <!---JOINS FOR GENRE/PLATFORM GO HERE--->
        WHERE
            <!---CONDITIONS GO HERE--->
      </cfquery>
    
      <cfreturn resultData>
    </cffunction>
    

    To figure out how many pages there are:

    totalNumOfPages = ceiling(countResult() / recordsPerPage);
    

    Any other question?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.