如何用2组值来划分数字..?

时间:2017-05-17 17:37:18

标签: php mysql

我根据from dateto date的用户输入执行了一些除法操作......

Mysql查询返回该特定日期的总和,我必须将该总和除以该月份的总天数。但问题是用户从日期和两个日期提供了2组输入。

如何将执行查询获得的总和除以2组值?

以下是我的PHP代码:

   <input id="monthIpone" name="monthone" type="month"> /*enters starting month*/
    <input id="monthIptwo" name="monthtwo" type="month"> /*enters ending month*/

      <input type="button"  value="submit" id="submit" class="btn btn-primary" name="submit" onclick="callphp()"> /*submit*/

这是我的计算部分:

    $fromdate = $_POST['monthone'];/* starting month */

    $todate = $_POST['monthtwo'];/* ending month */

    $split = explode('-', $fromdate);------------------------
                                                             |/* to 
                                                             | calculate    
    $year = $split[0];                                       | no of days
    $month = $split[1];                                      | present*/
    $days = cal_days_in_month(CAL_GREGORIAN, $month,$year);---

    $query = "SELECT DATE_FORMAT(`timestamp`, '%Y-%m') as timestamp, SUM(`gridpowertotal`) as sum FROM inverterlog WHERE inverter = '" . $inverter . "' AND DATE_FORMAT(`timestamp`, '%Y-%m') BETWEEN '" . $fromdate . "' AND '" . $todate . "' GROUP BY DATE_FORMAT(`timestamp`, '%Y-%m')";
    if ($result = mysqli_prepare($con, $query))
    {
       mysqli_stmt_execute($result);
       mysqli_stmt_bind_result($result, $timestamp, $sum);
       while(mysqli_stmt_fetch($result))
       {   
           $obj = new stdClass();
           $obj->timestamp = $timestamp;
           $obj->sum = $sum;
           array_push($monthArray,$obj);

       }

        $marray = array_values($monthArray);

        foreach($marray as $object)
         {
                $obj = new stdClass();
                $time = $object->timestamp;
                $obj->timestamp=$time;

                $total = $object->sum;
                $avg = $total/6;
                $cuf = $avg /((72*245)*(24*$days));/*need to caculate for 
                                                     both from date and two 
                                                     date*/
                $obj->cuf = $cuf;
                array_push($marray,$obj);

        }

1 个答案:

答案 0 :(得分:0)

下面是我的解决方案,只是将它移到了foreach循环中..

移动代码之前

   foreach($marray as $object)
     {
            $obj = new stdClass();
            $time = $object->timestamp;
            $obj->timestamp=$time;

            $total = $object->sum;
            $avg = $total/6;
            $cuf = $avg /((72*245)*(24*$days));/*need to caculate for 
                                                 both from date and two 
                                                 date*/
            $obj->cuf = $cuf;
            array_push($marray,$obj);

    }

在每个循环中移动代码后

      foreach($marray as $object)
        {
                $obj = new stdClass();
                $time = $object->timestamp;
                //var_dump($time);

                /*to find no of days in a month*/
                $split = explode('-', $time);
                $year = $split[0];
                $month = $split[1];
                $days = cal_days_in_month(CAL_GREGORIAN, $month,$year);
                //echo "$days"."<br/>";

                $obj->timestamp=$time;

                $total = $object->sum;
                $avg = $total/6;

                $cuf = $avg /((72*245)*(24*$days));
                $obj->cuf = $cuf;
                array_push($marray,$obj);

        }