It is a twitter like load more widget. My question is, is there a way when i reach the end of my database instead of it saying "load more" to say something like "we are done".
I believe that it would be here
Code:
//place the initial posts in the page
postHandler(initialPosts);
//first, take care of the "load more"
//when someone clicks on the "load more" DIV
var start = <?php echo $_SESSION['posts_start']; ?>;
var desiredPosts = <?php echo $number_of_posts; ?>;
var loadMore = $('#load-more');
//load event / ajax
loadMore.click(function(){
//add the activate class and change the message
loadMore.addClass('activate').text('Loading...');
//begin the ajax attempt
$.ajax({
url: 'load-more.php',
data: {
'start': start,
'desiredPosts': desiredPosts
},
type: 'get',
dataType: 'json',
cache: false,
success: function(responseJSON) {
//reset the message
loadMore.text('Load More');
//increment the current status
start += desiredPosts;
//add in the new posts
postHandler(responseJSON);
},
//failure class
error: function() {
//reset the message
loadMore.text('Oops! Try Again.');
},
//complete event
complete: function() {
//remove the spinner
loadMore.removeClass('activate');
}
});
});
});
The change would be on the PHP side to provide a message to the client (AJAX) that there were no more items to load. Then the client (AJAX) would need to check for that message to do what you would like it to do in that case.
Is there anyway you could point me in the right direction? The php is,
PHP Code:
<?php /* settings */ session_start(); $number_of_posts = 1; //5 posts will load at a time $_SESSION['posts_start'] = $_SESSION['posts_start'] ? $_SESSION['posts_start'] : $number_of_posts; //where we should start ?> <?php /* grab stuff */ function get_posts($start = 0, $number_of_posts = 5) { /* connect to and select the db */ $connection = mysql_connect(''); //hostname, username, password mysql_select_db('People',$connection); /* create the posts array */ $posts = array(); /* get the posts */ $query = "SELECT * FROM table1 WHERE style='time' AND webfilter LIKE '%Help%' ORDER BY table1.ID DESC LIMIT $start,$number_of_posts"; $result = mysql_query($query); /* for every post... */ while($row = mysql_fetch_assoc($result)) { /* set the post content equal to the first paragraph...a "preview" regular expression */ preg_match("/<p>(.*)<\/p>/",$row['makernotes'],$matches); $row['makernotes'] = strip_tags($matches[1]); /* add this record to the posts array */ $posts[] = $row; } /* return the posts in the JSON format */ return json_encode($posts); }
?> <?php /* loading of stuff */ if(isset($_GET['start'])) { /* spit out the posts within the desired range */ echo get_posts($_GET['start'],$_GET['desiredPosts']); /* save the user's "spot", so to speak */ $_SESSION['posts_start']+= $_GET['desiredPosts']; /* kill the page */ die(); }
You'll want to check if the response is empty prior to this line:
$.each(postsJSON,function(i,post) {
Or you can set a variable before that line to false and set it to true somewhere within the loop, then check if the variable is still false at the end of your function and you'll know if there were any responses.
Bookmarks