如何使用 MySQL 和 ColdFusion 创建分页功能
我正在尝试使用 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#¤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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的结果不是很大,您可以继续使用返回所有内容的相同 SQL,并
在显示它时使用,请参阅:http://www.coldfusionjedi.com/index.cfm/2006/4/24/ColdFusion-and-Pagination。不需要查询的查询。
回答你的问题
为什么要在同一个 cfinvoke 中返回两个变量?相反,编写 2 个函数: countResult() 和 getResultData(page, RecordsPerPage)
对于在数据库级别使用真正分页的 getResultData(page, RecordsPerPage):
如果要在数据库级别执行真正分页,请使用 LIMIT和MySQL 中的
OFFSET
。要计算有多少页:
还有其他问题吗?
If your results is not huge, you can stay with the same SQL that returns everything and use
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
WHY do you want to return two variables in the same cfinvoke? Instead, write 2 functions: countResult() and getResultData(page, RecordsPerPage)
For getResultData(page, RecordsPerPage) using true paging in DB level:
If you want to do true pagnation in DB level, use
LIMIT
andOFFSET
in MySQL.To figure out how many pages there are:
Any other question?
我之前没有这样做过(但不是在 MySQL 中),而不是使用两个函数和两个数据库调用:
它将一个名为“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):
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