However since i am not too familiar with oop in php. i am not sure what i need to define the $db variable as, in order to query my database properly. Is their a way i can edit this code, so it sidesteps the $db and uses a different approach, the code is using ajax to load more posts. The error is on line 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>
Load more
<?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>';
}
}
}
?>
This is going to prove complicated if you are not used to Object Oriented PHP but you need to construct your objects. The MySQLi class constructor for instance, takes user, password, database, etc and then knows where it should connect to.
Before you can use stuff like $db->get_results, you need to construct your DB object. Now I don’t recognize the get_results function so I presume it is a custom built DB class. You should see the documentation for that specific class if you want to know what to do.