dongni1892 2015-06-08 16:21
浏览 28
已采纳

当页面到达底部时,ajax和php从mysql加载更多内容

I've seen some answers to this question on this site already, but i still haven't been able to implement it as i want to for 2 days now. I'm sort of a beginner so a very clear explanation would be of great help,

i have a mysql database of some data, i want to read from a table and display 30 rows at a time, when the user scrolls to the end of the page, i want to load another 30 rows (i have been able to do the first 30, but loading the remaining is a challenge for me).

i already have this:

$(document).ready(function(){   
       $(window).scroll(function() {
           if($(window).scrollTop() == $(document).height() -$(window).height()) {
               //ive tried all sorts of things here it just doesnt work
           }
});

also an example of the php file that loads the next contents would help,

i'm using php and mysqli

thanks a lot in advance.

so this is my loadmore.php, its for the functionality, haven't styled the output:

<?php 
require_once 'functions.php'; //my databse connection is in this file

//i created a function queryMysql($query) in functions.php, thats what is used here 
$result = queryMysql("SELECT * FROM articles WHERE live='1' ORDER BY created DESC LIMIT $start, 30");
$num = $result->num_rows;

for ($j = 0 ; $j < $num ; ++$j){
    $row = $result->fetch_array(MYSQLI_ASSOC);
    $title = $row['title'];
    $subtitle = $row['subtitle'];
    echo "$title<br />$subtitle";                                                    
}?>

for the ajax, i changed it to the first answer i got here, but all my attempts have looked like this:

 $(window).scroll(function() {
                            if($(window).scrollTop() == $(document).height() - $(window).height()) {
                                $.ajax({
                                    type: method,
                                    data: {}, //Your data
                                    url: 'loadmore.php',
                                    async: true,
                                    success: function (data, textStatus, jqXHR) {
                                        $('#article-inro-hold').append(data);
                                    },
                                    error: function (jqXHR) {
                                        //Error handler
                                    }
                                });
                            }
                        });
  • 写回答

3条回答 默认 最新

  • dthy81285 2015-06-08 16:24
    关注

    Try to implement jquery ajax, something rough like this:

    $(document).ready(function () {
        $(window).scroll(function () {
            if ($(window).scrollTop() == $(document).height() - $(window).height()) {
                $.ajax({
                    type: method,
                    data: {}, //Your data
                    url: 'your/url/to/get/more/content/from',
                    async: true,
                    success: function (data, textStatus, jqXHR) {
                        $('#myDiv').append(data);
                    },
                    error: function (jqXHR) {
                        //Error handler
                    }
                });
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP