duan2477 2017-08-20 10:18
浏览 24
已采纳

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

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 2017-08-20 10:35
    关注

    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 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度