-3

如果你能帮助我,我有一个问题要问你,我有一些变量,我用它们从数据库中检索数据,但我想解决这个问题 if all the variables are empty then show something if not then show the just the variables that have data

该代码是:

foreach ($row as $result) {
    if ($row[29] == '') {
        $string29 = '';
    }else if ($row[29] == 0){
        $string29 = '';
    } else{
        $string29 = '<div class="add_img"><h1>Pression</h1><img src="images/'.$row[29].'.png"></div>';
    }
}

foreach ($row as $result) {
    if ($row[30] == '') {
        $string30 = '';
    }else if($row[30] == 0){
        $string30 = '';
    } else{
        $string30 = '<div class="add_img"><h1>Fixation</h1><img src="images/'.$row[30].'.png"></div>';
    }
}

`

然后我有 echo &string29 and so on........

4

1 回答 1

0

您的代码相当多余。通过 PHP 类型转换规则,0实际上''是松散相等的:

php > var_dump(0 == '');
bool(true)

你应该做的是有一个可以循环的键/名称数组,例如:

$fields = array(29 => 'Pression', 30 => 'Fixation');

foreach ($fields as $key => $field) {
   if ($row[$key]) { .... }
   echo '...';
}
于 2013-06-23T21:10:00.637 回答