【问题标题】:php Switch/case doesn't workphp 开关/外壳不起作用
【发布时间】:2017-02-15 10:07:37
【问题描述】:

我试图在另一个开关/案例问题中找到我的答案。但我没有找到解决方案。

我有一个开关,可以将我的日期值分成 4 个不同的部分。 但是当我想打印出来时,它不起作用。我不知道我做错了什么。

这是一个错字还是?

提前致谢。

我的代码

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>\n";
           print "  <td>" . $item['nrplaat'] . "\n";
           print "  <td>" . $item['tptcode'] . "\n";
           print "  <td>" . $item['chrononr'] . "\n";
           print "  <td>" . $item['projectcode'] . "\n";
           print "  <td>" . $item['projectnaam'] . "\n";
           print "  <td>" . $item['1eweging'] . "\n";
           print "  <td>" .  "<span class=\"status\">".$item['class']."</span>" ."\n";
           print "  <td>" . $item['overweighted'] . "\n";
           print "  <td>" . $item['date'] . "\n";
           print "  <td>" . $item['2eweging'] . "\n";
           print "  <td>" . $item['netto'] . "\n";
           print "</tr>\n";
           break;
    }                                    
}

【问题讨论】:

  • 你得到什么错误?
  • 没有错误。我很确定我的开关有问题

标签: php html switch-statement


【解决方案1】:

使用break;

case($date > $s_year.'-'.$quart1 && $date <= $s_year.'-'.$quart2):
    $item['quarter'] = '1';
    break;

【讨论】:

    【解决方案2】:

    可能是因为您在每个 switch 案例中都没有break;。 尝试添加一些break;

    请参阅here 在 php 中进行切换。

    【讨论】:

      【解决方案3】:

      您应该在每个案例的末尾添加一个break; 语句。

      【讨论】:

        【解决方案4】:

        您应该在“case”之后添加“break”。如果您有一些“案例”,这一点非常重要。请理解switch语句的概念,可以向http://php.net/manual/en/control-structures.switch.php学习。

        如果你有一些'case'并且你没有使用'break',这意味着下一个case也将继续进行。如果您在“case”之后使用“break”,则切换过程将完成并且不会继续下一个 case。也许您也应该了解“继续”:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-23
          • 2010-12-30
          • 2014-01-28
          相关资源
          最近更新 更多