P
P
prrrrrrr2019-05-06 16:38:09
API
prrrrrrr, 2019-05-06 16:38:09

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

2 answer(s)
A
Andrew, 2019-05-06
@deepblack

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>

We get the answer:
{
  "response": {
    "message": "Success",
    "result": {
      "RecordCount": 3280
    },
    "status": 0,
    "uri": "/api/forms/department/getRecordCount"
  }
}

Having a JSON structure like this:
{
  "records": [
    {
      "id": "1",
      "msg": "Success"
    },
    {
      "id": "2",
      "msg": "Failed"
    },
    {
      "id": "3",
      "msg": "In progress"
    },
    {
      "id": "4",
      "msg": "Pending"
    }
  ]
}

Get number of objects:
const obj = JSON.parse(...);
const recordsLength = Object.keys(obj.records).length;

Why is there no JavaScript in the question tags?
This question, in the wording in which it is, in no way concerns the API.

A
Alex-1917, 2019-05-06
@alex-1917

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 question

Ask a Question

731 491 924 answers to any question