I have a dynamic web dashboard which displays data like temp, humidity, light, noise etc. I have multiple php files like temp.php, humidity.php, light.php and noise.php which take care of retrieving the data from db and then I have multiple js files too which basically use setTimeout and displays data from the corresponding php file to the html page every 3 seconds.
Each of the php file looks like this, example- humidity.php:
<?php
session_start();
if(isset($_SESSION["user_id"])){
include('db.php');
$unit = "820";
$stmt = $db->prepare("SELECT hv FROM humidity where
unitid=? order BY pk DESC LIMIT 1");
$stmt->execute([$unit]);
$humidity= $stmt->fetchColumn();
$humidity=round($humidity, 2, PHP_ROUND_HALF_ODD);
echo $humidity;
$stmt->closeCursor();
$db = null;
}
?>
And each js file looks like this, example- humidity.js:
$(document).ready(function() {
function foo() {
$('#showhumidity').load('humidity.php');
setTimeout(foo, 3000);
}
foo();
});
The process is working fine, but since there are multiple php requests, the overall processing time is little high (around 2 seconds). I would like to combine the phps into one php file and the js files into one too - thereby having just one php request to retrieve all the data.
What is the best way to do it?