如何使用 MySQL 和 ColdFusion 创建分页功能

发布于 2024-08-24 21:03:56 字数 4589 浏览 3 评论 0原文

我正在尝试使用 MySQL 和 ColdFusion 为搜索结果创建分页。我的目的是只检索可以在单个页面上显示的查询,从而使过程高效。我尝试在函数中使用两个查询,但无法将两个变量返回到 cfinvoke。

以下代码不会分页,但它使用 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>

使用此逻辑,我可以显示我需要在页面上显示的内容:

<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>

如果我在同一页面上执行查询,那么遵循许多教程就会很容易。但查询是在函数中执行的。显示数据很容易,但分页却成了我的噩梦。这是我的函数:

<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>   

为了分页,我考虑将我的函数修改为以下内容(使用计数语句的新查询):

<!--- 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>

然后我想如果有结果要返回,我将执行嵌套查询并创建分页变量:

<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>

我想我会返回两个变量:resultCount 和 resultData。我将使用 #resultCount# 构建分页,并使用 #resultData# 显示输出。问题是我无法在同一个 cfinvoke 标记中返回两个变量。关于如何采取正确方法的任何想法?我完全不知道我需要遵循的逻辑。


编辑:我现在使用以下代码进行分页(唯一的问题是现在我必须将所有搜索过滤器重新传递回 URL,因为使用 #CGI.SCRIPT_NAME# 会清除它们):

<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>

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#¤tPage=#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#¤tPage=#currentPage+1#">Next Page</a>
     <cfelse>
          --
     </cfif>

</cfif>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最笨的告白 2024-08-31 21:03:56

如果您的结果不是很大,您可以继续使用返回所有内容的相同 SQL,并

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

在显示它时使用,请参阅:http://www.coldfusionjedi.com/index.cfm/2006/4/24/ColdFusion-and-Pagination。不需要查询的查询。

回答你的问题

问题是我无法退回两个
同一 cfinvoke 标记中的变量。

为什么要在同一个 cfinvoke 中返回两个变量?相反,编写 2 个函数: countResult() 和 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>

对于在数据库级别使用真正分页的 getResultData(page, RecordsPerPage):

如果要在数据库级别执行真正分页,请使用 LIMIT和MySQL 中的OFFSET

<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>

要计算有多少页:

totalNumOfPages = ceiling(countResult() / recordsPerPage);

还有其他问题吗?

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?

只怪假的太真实 2024-08-31 21:03:56

我之前没有这样做过(但不是在 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 *,
      (
        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>

它将一个名为“rowsFound”的列添加到返回的记录集中。
不是很正常化,但没什么大不了的。尽量减少数据库点击可能是值得的。

我认为它被称为“子查询作为标量操作数”:
http://dev.mysql.com/doc/refman/ 5.1/en/scalar-subqueries.html

托尼

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文