What I want is:
Let's take for a case that I have the following table:
| id | lux |
|------|-------|
| 1 | 22.5 |
| 2 | 23.5 |
| 3 | 25.5 |
| 4 | 27.5 |
| 5 | 23.5 |
| 6 | 24.5 |
| 7 | 24.5 |
| 8 | 22.5 |
| 9 | 29.5 |
| 10 | 21.5 |
Well, what I want is for the client to send the value of the id
plus the number of values to show from thatid
, I will explain myself with a better example:
Let's assume that we get the id from the client with a value of 4 and an amount of 5 , since I want the following 5 values to be returned to the given id , which the server should return to me:
| 4 | 27.5 |
| 5 | 23.5 |
| 6 | 24.5 |
| 7 | 24.5 |
| 8 | 22.5 |
The code I have so far with PHP is the following:
$servername = "localhost";
$username = "username";
$password = "password";
$db = "meteo";
$quantity = intval($_POST["quantity"]); //Este sería el valor de la id
$current = intval($_POST["current"]); //Este sería el valor de la cantidad
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
$sql = "SELECT temp1, temp2, lux, date FROM records";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$i = $current;
$param = array();
while ($row = $result->fetch_assoc()) {
if ($i >= $current+$quantity) {
break;
}
$param[$i] = array($row["temp1"],$row["temp2"],$row["lux"], $row["date"]);
$i++;
}
echo json_encode($param);
} else {
echo null;
}
}
As you can see, I have already tried to do what I wanted, but of course, I realized that when I consulted, for example, 100 new values in the client, it returned the same ones again , when in reality they should be the 100 values following the id
given by the client .
I would appreciate any help possible.
You can try with the following query