douyaosi3164 2017-02-15 10:07
浏览 124
已采纳

php Switch / case不起作用

I tried to find my answer in the other switch/case question. But i don't find the solution.

I got a switch that split my date values in 4 different quarters. But when i want to print it out, it doesn't work. I don't know what i'm doing wrong.

is this a typo or?

Thanks in advance.

MY CODE

while (odbc_fetch_row($result)) { // while there are rows 
    $overweight = odbc_result($result, "Weight1") - 44000;
    //$total_overweight += $overweight; 
    $date = date("Y-m-d", strtotime(odbc_result($result, "Date1")));
    $companies[] = odbc_result($result, "String3");
    $weight = odbc_result($result, "Weight1");                                     
    $item['nrplaat'] = odbc_result($result, "String1");
    $item['tptcode'] = odbc_result($result, "String3");
    $item['chrononr'] = odbc_result($result, "String15");
    $item['projectcode'] = odbc_result($result, "String4");
    $item['projectnaam'] = odbc_result($result, "String8");
    $item['1eweging'] = $weight;
    $item['overweighted'] = $overweight;
    $item['date'] = $date;
    $item['2eweging'] = odbc_result($result, "Weight2");
    $item['netto'] = odbc_result($result, "Nett");                                                                     
    switch($weight){
        case($weight > '44000' && $weight <= '44500'):
           $item['class'] = 'lichtgroen';
        case($weight > '44500' && $weight <= '45000'):
           $item['class'] = 'groen';
        case($weight > '45000' && $weight <= '46000'):
           $item['class'] = 'donkergroen';
        case($weight > '46000' && $weight <= '47000'):
           $item['class'] = 'bruingroen';
        case($weight > '47000' && $weight <= '48000'):
           $item['class'] = 'lichtbruin';
        case($weight > '48000' && $weight <= '49000'):
           $item['class'] = 'bruin';
        case($weight > '49000' && $weight <= '50000'):
           $item['class'] = 'lichrood';
        case($weight > '50000'):
           $item['class'] = 'rood';                                                                               
    }                                    
    switch($date){
        case($date > $s_year.'-'.$quart1 && $date <= $s_year.'-'.$quart2):
           $item['quarter'] = '1'; //kwartaal 1
        case($date > $s_year.'-'.$quart2 && $date <= $s_year.'-'.$quart3):
           $item['quarter'] = '2'; ////kwartaal 2
        case($date > $s_year.'-'.$quart3 && $date <= $s_year.'-'.$quart4):
           $item['quarter'] = '3'; ////kwartaal 3
        case($date > $s_year.'-'.$quart4 && $date <= $s_year.'-'.$end):
           $item['quarter'] = '4'; ////kwartaal 4                                        
    }
    //$item['quarter'] = 1; WHEN I DO THIS, ALL RESULTS WILL PRINT OUT!!!
    switch($item['quarter']){
        case '1': 
           print "<tr>
";
           print "  <td>" . $item['nrplaat'] . "
";
           print "  <td>" . $item['tptcode'] . "
";
           print "  <td>" . $item['chrononr'] . "
";
           print "  <td>" . $item['projectcode'] . "
";
           print "  <td>" . $item['projectnaam'] . "
";
           print "  <td>" . $item['1eweging'] . "
";
           print "  <td>" .  "<span class=\"status\">".$item['class']."</span>" ."
";
           print "  <td>" . $item['overweighted'] . "
";
           print "  <td>" . $item['date'] . "
";
           print "  <td>" . $item['2eweging'] . "
";
           print "  <td>" . $item['netto'] . "
";
           print "</tr>
";
           break;
    }                                    
}
  • 写回答

4条回答 默认 最新

  • duanjigua5753 2017-02-15 10:14
    关注

    Use break;

    case($date > $s_year.'-'.$quart1 && $date <= $s_year.'-'.$quart2):
        $item['quarter'] = '1';
        break;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥50 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗