但是因爲我不太熟悉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>';
}
}
}
?>
你有沒有試過尋找你從哪裏複製* *? – 2012-04-10 06:32:10
我建議不要盲目複製粘貼代碼來進行數據庫交互。 (a)不太可能滿足您的需求,(b)跳過您需要實際創建數據庫驅動應用程序的基本理解。 – Amber 2012-04-10 06:32:23
「我如何定義$ db?注意:未定義的變量:db」@我不知道。我們不能僅僅猜測你的代碼是什麼。如果您要複製並粘貼代碼,最好先理解它的作用。我建議閱讀OOP PHP。祝你好運。相對用戶名btw「come2gether」。確實。 – Flukey 2012-04-10 06:51:54