我希望通过 postgresql 数据库中的数据提取来完成页面滚动的自动加载。
以下是所需的结果:http://www.sanwebe.com/assets/ajax-load-on-scroll/
我认为问题位于 index.php 和/或 audoload_process.php 的Javascript代码中,但我不是能够找到问题。
我有3个文件:
正确运行的Config.php:
<?php
$db_username = 'user=myuser';
$db_password = 'password=mytest';
$db_name = 'dbname=test';
$db_host = 'host=localhost';
$items_per_group = 5;
$db = pg_connect($db_host, $db_username, $db_password, $db_name);
?>
Index.php文件:(我测试过它的PHP部分正在运行,我认为这是不起作用的javascript)。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>
<?php
include("config.php");
$get_total_rows = 0;
$db = pg_connect("$db_host $db_name $db_username $db_password");
$query = "SELECT * FROM paginate";
$results = pg_query($query);
$get_total_rows = pg_numrows($results);
//break total records into pages
$total_groups= ceil($get_total_rows/$items_per_group);
?>
<script type="text/javascript">
$(document).ready(function() {
var track_load = 0; //total loaded record group(s)
var loading = false; //to prevents multipal ajax loads
var total_groups = <?=$total_groups;?>; //total record group(s)
$('#results').load("autoload_process.php", {'group_no':track_load}, function() {track_load++;}); //load first group
$(window).scroll(function() { //detect page scroll
if($(window).scrollTop() + $(window).height() == $(document).height()) //user scrolled to bottom of the page?
{
if(track_load <= total_groups && loading==false) //there's more data to load
{
loading = true; //prevent further ajax loading
$('.animation_image').show(); //show loading image
//load data from the server using a HTTP POST request
$.post('autoload_process.php',{'group_no': track_load}, function(data){
$("#results").append(data); //append received data into the element
//hide loading image
$('.animation_image').hide(); //hide loading image once data is received
track_load++; //loaded group increment
loading = false;
}).fail(function(xhr, ajaxOptions, thrownError) { //any errors?
alert(thrownError); //alert with HTTP error
$('.animation_image').hide(); //hide loading image
loading = false;
});
}
}
});
});
</script>
<style>
body,td,th {font-family: Georgia, Times New Roman, Times, serif;font-size: 15px;}
.animation_image {background: #F9FFFF;border: 1px solid #E1FFFF;padding: 10px;width: 500px;margin-right: auto;margin-left: auto;}
#results{width: 500px;margin-right: auto;margin-left: auto;}
#resultst ol{margin: 0px;padding: 0px;}
#results li{margin-top: 20px;border-top: 1px dotted #E1FFFF;padding-top: 20px;}
</style>
</head>
<body>
<ol id="results">
</ol>
<div class="animation_image" style="display:none" align="center"><img src="ajax-loader.gif"></div>
</body>
</html>
autoload_process.php文件
<?php
include("config.php"); //include config file
if($_POST) //NOT SURE IF THIS WORKS FOR Postgresql or I would need somethingk like if ($_SERVER["REQUEST_METHOD"] == "POST")
{
echo "inside IF" //To see if it gets to here --> It doesn't
//sanitize post value
$group_number = filter_var($_POST["group_no"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
//throw HTTP error if group number is not valid
if(!is_numeric($group_number)){
header('HTTP/1.1 500 Invalid number!');
exit();
}
//get current starting point of records
$position = ($group_number * $items_per_group);
$query = "SELECT id, name, message FROM paginate ORDER BY id ASC LIMIT $position, $items_per_group";
$result = pg_query($query);
$myrow = pg_fetch_assoc($result);
$id = $myrow[id];
$name = $myrow[name];
$message = $myrow[message];
echo '<ul class="page_result">';
while(pg_fetch_assoc($results)){ //fetch values
echo '<li id="item_'.$id.'"><span class="page_name">'.$id.') '.$name.'</span><span class="page_message">'.$message.'</span></li>';
}
echo '</ul>';
pg_close();
}
?>
结果和问题
如果我在Index.php中编写此代码以查看是否正在检索信息:
$get_total_rows = pg_numrows($results);
echo "total number of rows" .$get_total_rows;
echo '<br>';
echo '<br>';
//break total records into pages
$total_groups= ceil($get_total_rows/$items_per_group);
echo "total number of groups" .$total_groups;
echo '<br>';
echo '<br>';
while($myrow = pg_fetch_assoc($results)) {
$id = $myrow[id];
$name = $myrow[name];
$message = $myrow[message];
echo $id;
echo $name;
echo $message;
echo '<br>';
echo '<br>';
}
然后我得到(总行数= 50(正确)组总数= 10(正确)和所有打印的记录但是当我向下滚动时我得到以下错误(内部服务器错误):小负载图像出现在页面的底部。(我猜这是因为我已经显示了所有50条记录,但它没有像它想象的那样表现。
任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
您的ajax .post中存在语法错误 - 单引号,不需要它们。这样:
$.post('autoload_process.php',{'group_no': track_load}
应该是这样的:
$.post('autoload_process.php',{group_no: track_load}
答案 1 :(得分:1)
最后缺少分号
echo "inside IF";
进行测试你可以尝试:
代替:
if($_POST)
使用强>
if(isset($_POST["group_no"]))
仅供测试,而不是
header('HTTP/1.1 500 Invalid number!');
<强>尝试强>
echo "Error not a number: " . $_POST["group_no"];
更改错误将允许您快速查看实际错误,而不是返回500并使其失败:内部服务器错误
这些可能无法解决问题,但至少有助于诊断。