drj26159 2017-06-02 04:02
浏览 123

将多个PHP文件的请求合并到一个请求中

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?

  • 写回答

1条回答 默认 最新

  • dtf24224 2017-06-02 04:50
    关注

    Hope below approach will help you.

    In your combined php file:

    <?php
      $humidity  = getHumidity(<parameter>);
      $temp = getTemp(<parameter>);
      $light = getLight(<parameter>);
      $retArr = array("humidity" => $humidity,"light" => $light, "temp" => $temp);
      echo json_encode($retArr);
    
      function getHumidity($param) { 
       // write your logic here to calculate the humidity
      }
    
      function getTemp($param) { 
       // write your logic here to calculate the temp
      }
    
      function getLight($param) { 
       // write your logic here to calculate the Light
      }
    
    ?>
    

    In your single .js file:

    jQuery(document).ready(function() {
        function foo() {
            jQuery.ajax({
                url : <path of your php file>,
                type : <Method GET/POST as per your requirement >,
                dataType : json,               
                async : false,
                success : function(data, status) {
                   //update your html element with data
                },
        }
    
    setInterval(foo, 3000);
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)