但是,由于我对 php 中的 oop 不太熟悉。我不确定我需要将 $db 变量定义为什么,以便正确查询我的数据库。他们是我可以编辑此代码的一种方式,因此它回避了 $db 并使用了不同的方法,该代码使用 ajax 加载更多帖子。错误在第 24 行
<html>
<head>
<script>
$(document).ready(function(){
$("#loadmorebutton").click(function (){
$('#loadmorebutton').html('');
$.ajax({
url: "loadmore.php?lastid=" + $(".postitem:last").attr("id"),
success: function(html){
if(html){
$("ul#posts").append(html);
$('#loadmorebutton').html('Load More');
}else{
$('#loadmorebutton').replaceWith('No more posts to show.');
}
}
});
});
});
</script>
</head>
<body>
<?php
if($posts = $db->get_results("SELECT id,text FROM posts ORDER BY id DESC LIMIT 10")) {
echo '<ul id="posts">';
foreach($posts as $post) {
echo '<li class="postitem" id="'.$post->id.'">'.$post->text.'</li>';
}
echo '</ul>';
}
?>
<button id="loadmorebutton">Load More</button>
</body>
</html>
装载更多
<?php
if($_GET['lastid']){
if($posts = $db->get_results("SELECT id,text FROM posts WHERE id < ".$db- >escape($_GET['lastid'])." ORDER BY id DESC LIMIT 10")) {
foreach($posts as $post) {
echo '<li class="postitem" id="'.$post->id.'">'.$post->text.'</li>';
}
}
}
?>