Answer the question
In order to leave comments, you need to log in
How to display the latest records from the database?
There is an output of all records from the database
xmlhttp.onload = function () {
if (xmlhttp.status === 200) {
var userInfo = JSON.parse(this.responseText);
console.log(this.responseText);
var arr = userInfo.results.reverse();
var text = "";
for (var i = 0; i < arr.length; i++) {
text += '<div class="blog-post"><div class="blog-header">'+arr[i].title + '</div><br><img width="560px;" src="' + arr[i].img + '"><br><div class="blog-text">' + arr[i].text + '</div><div class="blog-share"><a href="http://gamer-by-life.com/share/?title='+arr[i].title + '&text='+arr[i].text + '&img='+arr[i].img + '"><img src="share.png"></a></div><br></div><br>';
}
for (var i = 0; i < arr.length; i++) {
Answer the question
In order to leave comments, you need to log in
It would be more logical to change the number and order of records on the server side and not send useless bytes to the client. Otherwise, in the future, loading 10 visible records will drag along another 1000 unnecessary ones.
if (arr.length<10) from=10; else from=arr.length;
for (var i = from-10; i < arr.length; i++) {
Try:
for (var i = arr.length-10; i < arr.length; i++) {
...
}
if(arr.length > 10) {
for (var i = arr.length-10; i < arr.length; i++) {
...
}
}
else {
for (var i = 0; i < arr.length; i++) {
...
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question