duanquanzhi5560 2016-08-11 13:48
浏览 26
已采纳

Ajax加载更多功能而不是我需要的东西

I got a simple loadmore function, which sets the limit of a query and adds to the offset so only new items are loaded again. This works fine when I have 4 items to start with, but not when I want 8 to be displayed from the start.

My code:

var limit = 8;
var offset = 0;

$('#loadmoreprojects').click(function() {
    limit = 4;
    offset += 4;

    ajax();

});

Query:

$project                = "
SELECT ct.*, fe.*, cn.*
FROM 123_content ct
INNER JOIN 123_fieldsandfilters_elements fe on fe.item_id = ct.id
INNER JOIN 123_fieldsandfilters_connections cn on cn.element_id = fe.id
WHERE ct.catid IN ('10')
ORDER BY ct.ordering LIMIT ".$_POST['limit']." OFFSET ".$_POST['offset']."";

This shows 8 items from the start, but when loading more it shows the 2nd row again (the last 4 of the first 8 items). That makes sense because the limit is set to 4 again when clicking the button (because after the first 8, I only want 4 new items to be loaded) if I change it into 8, then 8 items are loaded on every click, which is not what I want.

How can I solve this?

  • 写回答

1条回答 默认 最新

  • douzhan2027 2016-08-11 14:08
    关注

    Solution:

    var limit = 4;
    var offset = 0;
    var i =0;
    $('#loadmorenews').click(function() {
        limit = 2;
        if(i == 0) {
            offset += 4;
        } else {
            offset += 2;
        }
        i++;
    
        ajax();
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了