switch语句中出现奇怪的结果

时间:2018-12-10 13:23:17

标签: mysql parsing switch-statement

预先感谢那些花时间看我的问题的人。我从描述承包商活动的表格中获取结果。有大量的True / False字段,我只想显示正确的字段。我创建一个field_name => field_type数组,并相应地解析我的显示。 (为大代码段表示歉意。)

$result = $mysqli->query($visitsSQL);
if ( $result->num_rows == 0 ){ // Visit records don't exist
    $_SESSION['message'] = "Cannot find any unpaid visits";
    header("location: error.php");
}
else { // unpaid visits exist
    $fields= array();
    while ($finfo = mysqli_fetch_field($result)){
    $fields [$finfo->name]= $finfo->type ; //this array holds field names -> field types    
    }  //finish fetching keys and types
    echo "<br>";
    echo "<br>Begin visit records<br>";
    echo "<div class='review_row'> </div>"; //this puts a thin line to separate visits

    while ($visit = $result->fetch_assoc()) {  //here's where each visit is compiled and sent to screen

        echo "<div class='review_row'>";  //this puts a thin line on the bottom to separate visits

     foreach ($fields as $k => $v) { 

        $field_name = $k;
        $field_type = $v;
        $field_data = $visit[$field_name];

        switch ($field_type) {

            case 1:   //The field is a True / False field
                if ($field_data ==1){  //show the field only if the value is set to True
                   echo "I am a YES/NO field called " . $field_name . " <br>";  //This line is a test 
                   echo "<span style='color: #009999;> - " . $field_name . "</span><br>";              
                }
                break;
            case 4:  //The field is a double numeric
                   if ($field_data != null) {       //show the field only if the value is set to True             
                echo "<span style='color: #b38f00';>" . $field_name . "</span>  " . $field_data . "<br>";  
                   }
                break;
            case 253: //A text field
                   if ($field_data != null) {                    
                echo "<span style='color: #b38f00';>" . $field_name . "</span>  " . $field_data . "<br>";  
                   }
                break;
            case 254: //a Datetime field
                   if ($field_data != null) {                    
                echo "<span style='color: #b38f00';>" . $field_name . "</span>  " . $field_data . "<br>";  
                   }
                break;
            default: { //field type is not represented above
              // do nothing so far  I take care of each field type above 
            }             
        }  //end of switch
    }  //end of foreach 
}  //end of while loop for the query results
echo "</div>";

} // if / else的末尾,它确定是否有要显示的记录

奇怪的结果出现在“情况1”的“ switch”语句中。我当前的测试记录有7个“是/否”字段,其值均设置为1。它们是

  • 准备晚餐
  • 清理晚餐食物
  • 购买杂货
  • 洗后折叠的衣服
  • 洗床上用品
  • 真空地毯
  • 席卷厨房的地板

对于每个“ field_type = 1”,我应该得到两个“ echo”:(1)我的测试回显和(2)我最终想要显示的内容,但是由于某些原因,它们交替显示-请参见下图。我已经为此进行了两天的尝试,也尝试是否也可以进行解析,但是我想出了这种“每秒第二”的结果。

screenshot of output from query

感谢您的任何想法或指导。

1 个答案:

答案 0 :(得分:0)

我已找到问题并予以纠正。它与“ span ...”标签有关。我用一个类替换了内联“ style = ...”。我在班级中设置颜色,然后在“班级:之后”部分删除了设置。