First of all, there are several ways you can do this. You can load all data at the same time and manipulate what to show using jQuery. In addition, you can gradually receive more data from the server, and I will give a suggestion on how to do this below (the former may have performance improvements). In addition, loading data gradually using jQuery will affect SEO.
As I wrote in my comment on your question, you can use JSON to return data from PHP to jQuery and include a variable indicating whether there is more data to retrieve. You can then check this variable to see if the "read more" link should be shown or not. Let's look at the server side of things first.
I donβt know how you want to βsplitβ your data, but for simplicity I just assume that the text will be turned off after 500 characters, after which another 500 will be loaded, etc.
const LOAD_PER_REQUEST = 500; // Load 500 characters per request $text = 'Lorem ipsum...'; // Fetched from database $requestNumber = (isset($_POST['requestNumber'])) ? ((int)$_POST['requestNumber']) : 0; // Use to calculate which data to return to jQuery // Calculate & generate data to return to jQuery $start = ($requestNumber * LOAD_PER_REQUEST); $length = ($start - ($start + LOAD_PER_REQUEST)); $newText = substr($text, $start, $length); $data = array( 'text' => $newText, 'hasMoreData' => (strlen($text) > ($start + LOAD_PER_REQUEST)) // If $text length is longer than where we cut off ); echo json_encode($data);
Now in jQuery, you can get the data by doing something like this:
$(function() { var requestNumber = 1; // Assuming that initial data is placed on the page with PHP $('#refreshFeed').on('click', function(){ // Get more data $.ajax({ type: 'POST', url: 'phpscripts/getFeed.php', dataType: 'json', data: 'requestNumber=' + requestNumber, success: function(result) { if (result.hasMoreData) { // There is more data; show "read more" $('#refreshFeed').show(); } else { // No more data; hide "read more" $('#refreshFeed').hide(); } requestNumber++; } }); }); });
I have not tested this code, but I hope you understand my idea.
source share