开发者

How to create a paging function with MySQL and ColdFusion

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>


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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜