Answer the question
In order to leave comments, you need to log in
How to calculate and display the number of records from the database using the API?
Can you tell me how to count API records in the database and display the number on the screen?
Following the example in MySql : mysqli_num_rows
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
jsonObject = JSON.parse(this.responseText);
let html = ''
for (let key in jsonObject.records) {
html += '<p>Уведомление</p>' + jsonObject.records[key].title;
}
document.getElementById('output').innerHTML = html;
}
};
xhttp.open("GET", "https://site.com/api.php/records/badge", true);
xhttp.send();
</script>
<div id="output"></div>
Answer the question
In order to leave comments, you need to log in
The question needs to be specified. What exactly is the complexity and with what API?
In order to get the number of records from the database, the API must have an appropriate endpoint.
Which you need to access and get the corresponding value.
For example, referring to
https://people.zoho.com/people/api/employee/counts?authtoken=<token>
{
"response": {
"message": "Success",
"result": {
"RecordCount": 3280
},
"status": 0,
"uri": "/api/forms/department/getRecordCount"
}
}
{
"records": [
{
"id": "1",
"msg": "Success"
},
{
"id": "2",
"msg": "Failed"
},
{
"id": "3",
"msg": "In progress"
},
{
"id": "4",
"msg": "Pending"
}
]
}
const obj = JSON.parse(...);
const recordsLength = Object.keys(obj.records).length;
The API should return the number of records requested, if not, the API is bullshit!
On the client, we consider handles, no options:
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
jsonObject = JSON.parse(this.responseText);
var size = 0;
let html = ''
for (let key in jsonObject.records) {
html += '<p>Уведомление</p>' + jsonObject.records[key].title;
size++;
}
document.getElementById('output').innerHTML = html;
//тут можно ловить size
}
};
xhttp.open("GET", "https://site.com/api.php/records/badge", true);
xhttp.send();
</script>
<div id="output"></div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question