如何以某种方式对post返回的数据求和

How to sum the data returned from post, one way or another

本文关键字:返回 post 数据 求和 方式      更新时间:2023-09-26

下面的代码基本上查询一个PHP文件,该文件查询数据库中的cpu_names(在我的测试用例中总共有4个)和每个cpu_names的相关负载。然后我需要取这四个CPU时间中的每一个,将它们加在一起,然后除以CPU的总数。这就得到了总CPU负载。因此,对于测试用例,这段代码总共升级了5个CPU仪表,其中一个是total。唯一的问题是,无论我用哪种方法,我都无法对这些帖子求和!谢谢。

setInterval(function() {
    fill_sum=0.0;
    $("canvas[name='gauge']").each(function(){
        var cpu_name=this.innerHTML;
        var ctx = this.getContext('2d');
        var padding = this.style.padding.replace(/[^0-9]+/, '');
        var w=this.width
        var h=this.height;
        //var fill_percent=.02;

        $.post("BH_Responder.php",
            {cpu_use: true, cpu_name: cpu_name},
            function(data, status){
                fill_percent = data;
                ctx.clearRect(0, 0, w, h);
                DrawGauge(ctx, w, h, fill_percent);

            }
        ).done(function() {
            fill_sum = fill_sum+parseFloat(fill_percent);
        });
    });
    alert(fill_sum);
    total_percent = fill_sum/<?php echo $num_rows_cpu; ?>;
    $("canvas[name='master_gauge']").each(function(){
        var ctx = this.getContext('2d');
        var padding = this.style.padding.replace(/[^0-9]+/, '');
        var w=this.width
        var h=this.height;
        //var fill_percent=.02;
        ctx.clearRect(0, 0, w, h);
        DrawGauge(ctx, w, h, total_percent);
    });
}, 100);

PHP脚本部分:

}elseif (isset($_POST['cpu_use']) == true && empty($_POST['cpu_use'])==false && $_POST['cpu_use']==true){
    $cpu_name = $_POST['cpu_name'];
    $sql= mysql_query("SELECT used FROM tbl_cpu_use WHERE cpu_name='$cpu_name' ORDER BY id DESC LIMIT 1;");
    echo (mysql_num_rows($sql) != 0) ? mysql_result($sql,0,'used') : NULL;
}

对于AJAX、回调和所有这些的一个很好的解释

看起来你几乎有。done(),但你需要把所有这些东西:

total_percent = fill_sum/<?php echo $num_rows_cpu; ?>;
$("canvas[name='master_gauge']").each(function(){ 
    ...
    DrawGauge(ctx, w, h, total_percent);
});

中的.done()回调,但只调用它的最后一个ajax响应。要么跟踪你在ajax请求之前设置的计数器,要么跟踪你在PHP端会发出多少请求。

类似:

var totalCpus = <?php echo $numCpus; // assuming you're using PHP to echo out the gauges in
// the first place. otherwise use ?>
var totalCpus = $("canvas[name='gauge']").length; // use this.
// assuming of course, that the # of gauges is the same throughout each interval
setInterval(function() {
    fill_sum=0.0;
    $("canvas[name='gauge']").each(function(){
        var numDoneRequests = 0; // this one keeps track of *dun dun dunnn* the number of requests that have completed
        var cpu_name=this.innerHTML;
        ...
        ).done(function() {
            fill_sum = fill_sum+parseFloat(fill_percent);
            numDoneRequests++;
            if(numDoneRequests === totalCpus) {
                total_percent = fill_sum/<?php echo $num_rows_cpu; ?>;
                $("canvas[name='master_gauge']").each(function(){
                    var ctx = this.getContext('2d');
                    ...
                    DrawGauge(ctx, w, h, total_percent);
                });
        ...
    alert(fill_sum);
}, 100);

还有恐惧@alfasin的朋友,小Bobby Tables。当你做

$sql= mysql_query("SELECT used FROM tbl_cpu_use WHERE cpu_name='$cpu_name' ORDER BY id DESC LIMIT 1;");
                                                                ^^^^^^^^^

你将完全未经检查的用户输入插入到一个查询中,这个查询将进入你的数据库,并将你自己打开SQL注入。我意识到这可能是一个人为的例子,如果是的话,我很抱歉指出了一些你已经知道的事情。

(另外,请查看"为什么我不应该't在PHP中使用mysql_*函数?",因为你不应该使用mysql_库连接到数据库和一些漂亮的替代方案)