简体   繁体   English

while循环与if语句不能正常工作

[英]while loop with if statement not working properly

hi i have the following while loop : 嗨,我有以下while循环:

while ($row = mysqli_fetch_array($result2)){

    $elem_filho = $row['itempt_dim'];

    $marcas = "{name: " . "'" . $elem_filho . "'" . ", color: " . "'" . $cor_alea . "'" . ", size: 1";

    if (mysqli_num_rows($result2)) {
      $dif = "},";
    }

    else {
      $dif = "}]";
    }

    $vlk = $marcas . $dif;
    print_r($vlk);

  }

the thing is i had similar code and it worked this way, but the output is alway }, and i want the last item of the query to end with }] i can't see what i am doing wrong 事情是我有类似的代码,它以这种方式工作,但是输出始终是},并且我希望查询的最后一项以}结尾]我看不到我在做什么

thanks in advance 提前致谢

With json_encode you code is simplified to: 使用json_encode您的代码可简化为:

$vlk = [];
while ($row = mysqli_fetch_array($result2)) {
    $vlk[] = [
        'name' => $row['itempt_dim'],
        'color' => $cor_alea,
        'size' => 1,
    ];
}
print_r(json_encode($vlk));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM