如果从其他列计算列分数,则从列分数求和

时间:2016-02-24 08:18:19

标签: php mysql

你好,我有一个表KPI,我想从KPI中的这个类别角度计算最终得分(见图)

Table Screen Shoot

我手动编写脚本以保持分数列动态更新,但我需要编写总分,以自动调整我的条形量表。

这是我的一些代码:

$kc= mysql_query("SELECT * FROM kpidetindv WHERE 

     idCat='3' AND year='$yr' AND vis='0' AND idEmp='$np' ORDER BY code ASC");

While ($kc2= mysql_fetch_assoc($kc)) 

     {

        $ikdi = $kc2['idKpiDetIndv'];
        $ic = $kc2['idCat'];
        $iu = $kc2['idUnit'];

        $ictg= mysql_query("select * from `kpicat` where idCat='$ic'") or die(mysql_error());
        $ictg2= mysql_result($ictg, 0, 'code');

        $imt= mysql_query("select * from `kpiunit` where idUnit='$iu'") or die(mysql_error());
        $imt2= mysql_result($imt, 0, 'code');
        $imt3= mysql_result($imt, 0, 'symbol');

        $sum = mysql_query("SELECT sum(actual) FROM monthkpimontindv WHERE idKpiDetIndv='$ikdi'") or die(mysql_error());

          while ($row = mysql_fetch_array($sum)) {

              $mkmi= $row['sum(actual)']; }

        if ($mkmi=='') { $mkmi2='0';} else { $mkmi2=$mkmi; }

        $tg= $kc2['target'];
        $scr= $mkmi/$tg;
        $scr2= number_format($scr * 100, 0);

        $wgh= $kc2['weight'];
        $scr3= $scr2*$wgh/100;

        $trd= mysql_query("select * from `monthkpimontindv` where idKpiDetIndv='$ikdi' order by idMontIndv desc limit 1") or die(mysql_error());
        $trd2= mysql_result($trd, 0, 'actual');

        $trd3= mysql_query("select * from `monthkpimontindv` where idKpiDetIndv='$ikdi' order by idMontIndv desc limit 1,1") or die(mysql_error());
        $trd4= mysql_result($trd3, 0, 'actual');

        if ($trd2 > $trd4 ) { $trd5="trendup.png"; }
        else if ($trd2 < $trd4 ) { $trd5="trenddown.png"; }
        else if ($trd2 == $trd4 ) { $trd5="trendnet.png"; }

   echo '<tr>';
   echo '<td>'.$kc2['code'];
   echo '<td><a href="_viewKpi.php?kd='.$kc2["idKpiDetIndv"].'&&mtr='.$kc2['idMont'].' '" target="_Blank">'.$kc2['title'].'</a>';
   echo '<td>'.number_format($kc2['target']).' '.$imt3;
   echo '<td>'.number_format($mkmi2).' '.$imt3;
   echo '<td>'.$kc2['weight'];
   echo '<td>'.number_format($scr3, 0);
   echo '<td class="text-center"><img class=user-avatar src=assets/img/'.$trd5.'>';
   echo '<td class="text-center"><a href="editKpi.php?kd='.$kc2["idKpiDetIndv"].'" target="_Blank"><i class="fa-edit s16"></i></a> | <a href="sys/engine/delKpi.php?kd='.$kc2["idKpiDetIndv"].'&&dt='.$today.'&&pg='.$np.'&&clt='.$kc2["code"].'&&swch=1" onclick=\'return confirm("Are sure want to delete?")\'><i class="fa-trash s16"></i></a>';

1 个答案:

答案 0 :(得分:0)

                <?PHP 

                $kc= mysql_query("SELECT * FROM kpidetindv WHERE idCat='$qki7' AND year='$yr' AND vis='0' AND idEmp='$np' ORDER BY code ASC");

                    While ($kc2= mysql_fetch_assoc($kc)) 

                        {

                            $sum = mysql_query("SELECT sum(actual) FROM monthkpimontindv WHERE idKpiDetIndv='$q'") or die(mysql_error());

                            while ($row = mysql_fetch_array($sum)) {

                                $mkmi= $row['sum(actual)']; }

                                if ($mkmi=='') { $mkmi2='0';} else { $mkmi2=$mkmi; }

                                $tg= $kc2['target'];
                                $scr= $mkmi/$tg;
                                $scr2= number_format($scr * 100, 0);

                                $wgh= $kc2['weight'];
                                $scr3= $scr2*$wgh/100;

                                $fscr+= number_format($scr3, 0);
                        }

                                if ($fscr == 0) { $imgs="speedo.png";}
                                else if ($fscr <= 10 ) { $imgs="speedo1.png";}
                                else if ($fscr <= 20 ) { $imgs="speedo2.png";}
                                else if ($fscr <= 30 ) { $imgs="speedo3.png";}
                                else if ($fscr <= 40 ) { $imgs="speedo4.png";}
                                else if ($fscr <= 50 ) { $imgs="speedo5.png";}
                                else if ($fscr <= 60 ) { $imgs="speedo6.png";}
                                else if ($fscr <= 70 ) { $imgs="speedo7.png";}
                                else if ($fscr <= 80 ) { $imgs="speedo8.png";}
                                else if ($fscr <= 90 ) { $imgs="speedo9.png";}
                                else if ($fscr <= 1000 ) { $imgs="speedo10.png";}
            ?>