duan2477
duan2477
2017-08-20 10:18

如果变量从数组获得结果,则为未定义的变量

已采纳

I have a defined array. If is one from column (e.g. Bayern 'by') empty, then i do not receive variable $row_wohn_by and after in result get is: Notice: Undefined variable: row_wohn_by

Simply erase is not a solution for another operate. Expected is defined $row_wohn_by with 0 (zero) number.

Here is code:

$states = array(
    'Baden-Württemberg' => 'bw',
    'Bayern' => 'by',
    'Berlin' => 'be',
    'Thüringen' => 'th'
    );

    $numb_rows = mysqli_query($conn, 'SELECT COUNT(*)
                                        FROM members
                                        WHERE priv_staat = "Deutschland"
                                        ');
        $numb_row = mysqli_fetch_array($numb_rows);
        $total_wohn = $numb_row[0];

    $query_wohn = mysqli_query($conn, "SELECT `priv_land`, COUNT(*) AS `count` FROM `members` WHERE `priv_land` !=0 GROUP BY `priv_land`");
        while ($item_wohn = $query_wohn->fetch_assoc()) {
            ${'row_wohn_'.$states[$item_wohn['priv_land']]} = $item_wohn['count'];
            ${'row_wohn_per_'.$states[$item_wohn['priv_land']]} = number_format((($item_wohn['count'] / $total_wohn)*100), 2, ',', ' '); // calulate in %
    }

    $row_wohn_all = $total_wohn-($row_wohn_bw + $row_wohn_by + $row_wohn_be + $row_wohn_th);
    $row_wohn_per_all = number_format((($row_wohn_all / $total_wohn)*100), 2, ',', ' ');  // calulate in %

Thanks for help.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doubi7306 doubi7306 4年前

    You wrote:

    Expected is defined $row_wohn_by with 0

    Looking at your query (SELECT priv_land, COUNT(*) AS count FROM members), this can only be the case when there are no rows with priv_land = 'by'. So within your while-loop just after your query, that variable never got set.

    Solution: initialize all variables with value 0 and overwrite those with the results from your query:

    foreach($states as $privLand) {
        ${'row_wohn_' . $privLand} = 0;
        ${'row_wohn_per_' . $privLand} = 0;
    }
    
    $query_wohn = mysqli_query($conn, "SELECT `priv_land`, COUNT(*) AS `count` FROM `members` WHERE `priv_land` !=0 GROUP BY `priv_land`");
    while ($item_wohn = $query_wohn->fetch_assoc()) {
        ${'row_wohn_'.$states[$item_wohn['priv_land']]} = $item_wohn['count'];
        ${'row_wohn_per_'.$states[$item_wohn['priv_land']]} = number_format((($item_wohn['count'] / $total_wohn)*100), 2, ',', ' '); // calulate in %
    }
    

    For bonus points, rewriting everything to an array-notaition makes life a lot easier (example for only $row_wohn_*):

    // Initialize all keys with value 0
    $row_wohn = array_fill_keys($states, 0);
    
    $query_wohn = mysqli_query($conn, "SELECT `priv_land`, COUNT(*) AS `count` FROM `members` WHERE `priv_land` !=0 GROUP BY `priv_land`");
    while ($item_wohn = $query_wohn->fetch_assoc()) {
        $row_wohn[ $states[$item_wohn['priv_land']] ] = $item_wohn['count'];
    }
    
    $row_wohn_all = $total_wohn - array_sum( $row_whon );
    
    点赞 评论 复制链接分享