简单的分页

时间:2014-01-11 16:16:21

标签: mysql coldfusion

最近我一直在搞乱ColdFusion论坛脚本,我正在尝试为它添加分页,这样它每页只显示10条评论而不是全部。可悲的是,我只发现了复杂的解决方案来做到这一点,并没有像我在PHP中习惯的简单解决方案。目前,这是我从我的数据库中获取评论的代码:

<cfquery name = "comments" datasource = "#DSN#">
SELECT * 
FROM  `forum_comments` 
WHERE topicid = #id#
</cfquery>

因为我不希望在为每个主题提取1000多条评论时超载我的处理器,所以我想将所有评论分成每页10条评论的页面。只需对我的页面进行一些修改,这是否可行?我最好的猜测是使用MySQL中的URL.page语句和LIMIT函数,但我不知道如何。

更新

我目前的所有代码:

<cfif IsDefined('URL.page')> // Pagination
  <cfset page = URL.page>
<cfelse>
  <cfset page = 1>
</cfif>

<cfset howManyRecsToShow = 10>
<cfset startRec = page*howManyRecsToShow>


<cfquery name = "comments" datasource = "#DSN#"> // Get all comments
SELECT * 
FROM  `forum_comments` 
WHERE topicid = #id#
LIMIT #startRec#, #howManyRecsToShow#
</cfquery>




                <cfset colour ="post_uneven"> // Required for CSS

  <cfloop query="comments"> // Loop over comments


    <div id="post_text">

        <div id="post_text_edit" title="Edit"></div>
        <div id="post_text_delete" title="Delete"></div>

        <div id="post_text_title">
        RE: #gettopic.title#
        </div>

        <div id="post_text_date">
        #DateFormat(dateAdd("s", comments.timestamp, "01/01/1970"))#       #TimeFormat(dateAdd("s", comments.timestamp, "01/01/1970"))#
        </div>

        <div id="post_text_text">
        #comments.text#

        </div>

    </div>

</div>
                    </cfloop>

    </cfoutput>



    <cfif colour is "post_uneven"><cfset colour="post_even"><cfelse><cfset colour="post_uneven"></cfif> // Required for CSS




<div id="topic_info_balk">
<div id="forum_paginas">
   <cfif page gt 1>
    <a href="pageTest.cfm?page=#page-1#">PREVIOUS 10</a>&nbsp;|&nbsp;
  </cfif>
  <a href="?page=<cfoutput>#page+1#</cfoutput>">NEXT 10</a>
</div>

1 个答案:

答案 0 :(得分:3)

你说你想要一个简单的例子,所以在这里。我简单地说,这只会显示NEXTPREVIOUS个链接。如果你想要显示页数的稍微复杂的分页,那么你必须事先得到总记录数。 (我很快就用手工输入了这个,而不是现在在我的CF服务器上,所以不确定这是否100%准确)...

(pageTest.cfm)

<cfif IsDefined("url.page")>
  <cfset page = url.page>
<cfelse>
  <cfset page = 1>
</cfif>

<cfset howManyRecsToShow = 10>
<cfset startRec = page*howManyRecsToShow>

<cfquery name="q1">
  select id, username, email
  from users
  order by id limit #startRec# , #howManyRecsToShow#
</cfquery>

<cfoutput>
  <table border="1">
    <cfloop query="q1">
        <tr>
            <td>#q1.id#</td>
            <td>#q1.username#</td>
            <td>#q1.email#</td>
        </tr>
    </cfloop>
  </table>
  <cfif page gt 1>
    <a href="pageTest.cfm?page=#page-1#">PREVIOUS 10</a>&nbsp;|&nbsp;
  </cfif>
  <a href="pageTest.cfm?page=#page+1#">NEXT 10</a>
</cfoutput>