MySQL + PHP分页没有偏移

时间:2013-05-24 13:12:21

标签: mysql paginator

好的,这是我的分页代码:

    $SQL = "SELECT
            cpc.product_id,
            cp.product_internal_ref,
            cp.product_name,
            cpa.product_sale_price,
            cpa.is_product_service,
            cpa.product_service_price,
            cpi.image_name,
            cpi.image_ext
            FROM catalog_products_categories cpc 
            JOIN catalog_products cp ON cp.product_id = cpc.product_id
            JOIN catalog_products_attributes cpa ON cpa.product_id = cpc.product_id
            LEFT JOIN catalog_products_images cpi ON cpi.product_id = cpc.product_id
            WHERE cpc.category_id = ".$catID;

    // PAGINATOR SECTION
    if($paginatorVARS['paginatorACTION'] == "next") {
      $SQL .= " AND cpc.product_id > ".$paginatorVARS['paginatorGOID']." ";
      $SQL .= "GROUP BY cpc.product_id ORDER BY cpc.product_id ASC LIMIT ".$paginatorVARS['catalogPaginatorPAGEROWS'];
    }
    elseif($paginatorVARS['paginatorACTION'] == "prev") {
      $SQL .= " AND cpc.product_id < ".$paginatorVARS['paginatorGOID']." ";
      $SQL .= "GROUP BY cpc.product_id ORDER BY cpc.product_id DESC LIMIT ".$paginatorVARS['catalogPaginatorPAGEROWS'];
    }
       // END PAGINATOR SECTION

我使用了这里描述的方法:http://www.slideshare.net/Eweaver/efficient-pagination-using-mysql但我无法找到一种方法来排序其他列,例如cpa.product_sale_price。如果我这样做:ORDER BY cpc.product_id ASC,cpa.product_sale_price DESC / ASC它将打破分页器下一步结果我不知道会发生什么......请帮助!!

1 个答案:

答案 0 :(得分:1)

不是说他们的东西是完美的,但它通常是一个开始的地方,考虑你是否真的想重新发明轮子,或采取他们的概念并将其融入你的新轮组。看看

http://dev.sencha.com/deploy/ext-4.0.0/examples/grid/paging.html

并使用fiddler检查分页期间发生的http获取以及排序上下文中的更改。当它开始时它会带回http响应头,带有jsonp回调的json数据,以及json数据末尾的顶部节点值,指定每页记录计数= 6679/50,这意味着前端计算的总页数为134页。在插入和删除行时,记录计数可能因呼叫而异(从页面到页面)。

sort列在查询字符串中传递,就像页面影响限制开始一样,总计进入sql。这可以在fiddler输出中看到,显示页面更改或排序列请求(列名称,asc或desc):

GET /forum/topics-browse-remote.php?_dc=1369401925806&page=1&start=0&limit=50&sort=lastpost&dir=DESC&callback=Ext.data.JsonP.callback1 HTTP/1.1

GET /forum/topics-browse-remote.php?_dc=1369401977137&page=1&start=0&limit=50&sort=replycount&dir=ASC&callback=Ext.data.JsonP.callback2 HTTP/1.1

GET /forum/topics-browse-remote.php?_dc=1369401978355&page=1&start=0&limit=50&sort=replycount&dir=DESC&callback=Ext.data.JsonP.callback3 HTTP/1.1