循环

时间:2018-05-29 09:19:57

标签: php algorithm

我无法在我从数据库中获取数据的while循环中正确递增变量。

这是代码:

while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
            //January
            if($row['month'] ==1){
                if($row['day_range'] == '01-07'){
                    $val1_1 = $row['duration'];
                    $int1_1 = $row['intensity'];
                }
                elseif($row['day_range'] == '08-14'){
                    $val1_2 = $row['duration'];
                    $int1_2 = $row['intensity'];
                }
                elseif($row['day_range'] == '15-21'){
                    $val1_3 = $row['duration'];
                    $int1_3 = $row['intensity'];
                }
                elseif($row['day_range'] == '22-end'){
                    $val1_4 = $row['duration'];
                    $int1_4 = $row['intensity'];
                }
                //Avg intensity
                $int1 = ($int1_1 + $int1_2 + $int1_3 + $int1_4)/4;
            }
}

所以我每个月都有这个代码 这里的问题是,有时我没有4个值,所以最后当我计算AVG时,它有时是错误的,因为它总是将它除以 4

我做了什么:

每次我有一个值时,我就有了增加变量$ i的想法,比如这个

$i = 0;
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
            //January
            if($row['month'] ==1){
                if($row['day_range'] == '01-07'){
                    $val1_1 = $row['duration'];
                    $int1_1 = $row['intensity'];
                    $i++;
                }
                elseif($row['day_range'] == '08-14'){
                    $val1_2 = $row['duration'];
                    $int1_2 = $row['intensity'];
                    $i++;
                }
                elseif($row['day_range'] == '15-21'){
                    $val1_3 = $row['duration'];
                    $int1_3 = $row['intensity'];
                    $i++;
                }
                elseif($row['day_range'] == '22-end'){
                    $val1_4 = $row['duration'];
                    $int1_4 = $row['intensity'];
                    $i++;
                }
                //Avg intensity
                $int1 = ($int1_1 + $int1_2 + $int1_3 + $int1_4)/$i;
            }
            $i=0;
            //code for next month

}

但是它不起作用,我已经回应了它并且$i保持在1。 我认为这是因为它逐行获取所以它永远不会通过所有增量来达到所需的值。

我该怎么办呢?

2 个答案:

答案 0 :(得分:3)

// Here is an array to get the data of EACH month
$month = array();
$i = 0;
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
    // This way, you will have $month[1] for January, $month[2] for February, etc.
    $month[$row['month']][] = array(
        "duration" => $row['duration'],
        "intensity" => $row['intensity'];
    );
}

// Now you get all the data, you can calculate the Avg intensity for each month
foreach ($month as $month_number => $data) {
    $moy = count($month[$month_number]); // Will be 4 if you have 4 period, 3 if only 3, etc.
    $sum = 0;

    foreach ($data as $value) {
        $sum += $value['intensity'];
    }

    $month[$month_number]['avg_intensity'] = $sum / $moy;
}

使用此方法,您应该获得一个包含所需数据的数组:

$month = array(
    // January
    1 => array(
        0 => array(
            'duration' => ...,
            'intensity' => ...
        ),
        1 => array(
            'duration' => ...,
            'intensity' => ...
        ),
        ...
        'avg_intensity' => /* moy of all intensity of the month */
    ),
    // February
    2 => array(
        ...
    ),
    ...
);

希望它可以帮到你!

编辑:

正如Nigel Ren的评论建议的那样,你可以替换

$sum = 0;
foreach ($data as $value) {
    $sum += $value['intensity'];
}

通过

$sum = array_sum(array_column($data, "intensity"));

答案 1 :(得分:0)

请尝试

 $int1_1 += $row['intensity'];

方式。 这意味着

  $i = 0;
$int1_1 = 0;
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
            //January
            if($row['month'] ==1){
                if($row['day_range'] == '01-07'){
                    $val1_1 = $row['duration'];
                    $int1_1 += $row['intensity'];
                    $i++;
                }
                elseif($row['day_range'] == '08-14'){
                    $val1_2 = $row['duration'];
                    $int1_1 +=  $row['intensity'];
                    $i++;
                }
                elseif($row['day_range'] == '15-21'){
                    $val1_3 = $row['duration'];
                    $int1_1 +=  = $row['intensity'];
                    $i++;
                }
                elseif($row['day_range'] == '22-end'){
                    $val1_4 = $row['duration'];
                    $int1_1 +=  $row['intensity'];
                    $i++;
                }
                //Avg intensity
                $int1 = $int1_1/$i;
            }
            $i=0;
            //code for next month

}