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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP