views:

238

answers:

2

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>
+5  A: 

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?

Henry
Very well stated Henry! :-)
Dan Sorensen
Thanks, Henry, that's really helpful. The only thing is I'm still confused about where the variables should go for creating the pages. The second method you listed, can you think of any tutorials that are handy? The reason I asking is well, it's fine, I'm confused on creating the relationship between the two functions... or how to transfer variables from one function to the other (the resultCount to the getResultData)... sorry if it seems like an obtuse comment, but I'm learning too much too quickly and I can't keep up!
Mel
What do you mean by "where the variables should go for creating the pages"?
Henry
"can you think of any tutorials that are handy?" tutorial on what?
Henry
"(the resultCount to the getResultData)" getResultData does not need resultCount though. What do you mean?
Henry
you might want to break up those questions into individual questions. We're having a hard time understanding what you want. :)
Henry
Henry, many thanks once more. It works well. Sorry about the lengthy questions and the lengthier code. A final note: It's working well, but I have to pass my search filters manually back into the URL scope. Check the bottom of my question. I just added the code I was using. Is this healthy? It seems like anyone can just punch in a number of break the code. What do you think?
Mel
It's not wrong to use URL scope like this especially it is just getting, and not modifying any data on server or any side effects, but it'd be safer if you Val() the numeric URL variables, and URLEncodedFormat() the string URL variables. To make stateless web apps to have stateful, look into using the SESSION scope. CF Dev Guide is your friend.
Henry
Thanks again, Henry! Great stuff!
Mel
+1  A: 

Rather than have two functions and two database calls, I've done it like this before (not in MySQL however):

<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 *,
      (
        SELECT COUNT(gameID) AS rowsFound
        FROM
          <!---JOINS FOR GENRE/PLATFORM GO HERE--->
        WHERE
          <!---CONDITIONS GO HERE--->
      ) AS rowsFound
      LIMIT #recordsPerPage# OFFSET #offset#
        <!---JOINS FOR GENRE/PLATFORM GO HERE--->
    WHERE
        <!---CONDITIONS GO HERE--->
  </cfquery>

  <cfreturn resultData>
</cffunction>

It adds a column to the returned recordset called 'rowsFound'. Not very normalized, but not a big deal. Might be worth it to minimize the DB hits.

I think it's referred to as 'subquery as a scalar operand': http://dev.mysql.com/doc/refman/5.1/en/scalar-subqueries.html

Tony

Tony Brandner