0

我的代码从数据库中选择,写出信息然后运行一个 else if 循环来决定输出什么代码:

<?php

echo '<p>the loop<p><br>
 <p>Accreditations</p>';

      if ($fields['accreditations1']==1){echo 'HTML CODE 1';} else { 
      if ($fields['accreditations1']==2){echo 'HTML CODE 2';} else { 
      if ($fields['accreditations1']==3){echo 'HTML CODE 3';} else { 
      if ($fields['accreditations1']==4){echo 'HTML CODE 4';} else { 
      if ($fields['accreditations1']==5){echo 'HTML CODE 5';} else { 
      if ($fields['accreditations1']==6){echo 'HTML CODE 6';} else { 
      if ($fields['accreditations1']==7){echo 'HTML CODE 7';} else { 
      if ($fields['accreditations1']==0){echo '';}

      if ($fields['accreditations2']==1){echo 'HTML CODE 1';} else { 
      if ($fields['accreditations2']==2){echo 'HTML CODE 2';} else { 
      if ($fields['accreditations2']==3){echo 'HTML CODE 3';} else { 
      if ($fields['accreditations2']==4){echo 'HTML CODE 4';} else { 
      if ($fields['accreditations2']==5){echo 'HTML CODE 5';} else { 
      if ($fields['accreditations2']==6){echo 'HTML CODE 6';} else { 
      if ($fields['accreditations2']==7){echo 'HTML CODE 7';} else { 
      if ($fields['accreditations2']==0){echo '';}

      if ($fields['accreditations3']==1){echo 'HTML CODE 1';} else { 
      if ($fields['accreditations3']==2){echo 'HTML CODE 2';} else { 
      if ($fields['accreditations3']==3){echo 'HTML CODE 3';} else { 
      if ($fields['accreditations3']==4){echo 'HTML CODE 4';} else { 
      if ($fields['accreditations3']==5){echo 'HTML CODE 5';} else { 
      if ($fields['accreditations3']==6){echo 'HTML CODE 6';} else { 
      if ($fields['accreditations3']==7){echo 'HTML CODE 7';} else { 
      if ($fields['accreditations3']==0){echo '';}

      echo '<br />';

      if ($fields['accreditations4']==1){echo 'HTML CODE 1';} else { 
      if ($fields['accreditations4']==2){echo 'HTML CODE 2';} else { 
      if ($fields['accreditations4']==3){echo 'HTML CODE 3';} else { 
      if ($fields['accreditations4']==4){echo 'HTML CODE 4';} else { 
      if ($fields['accreditations4']==5){echo 'HTML CODE 5';} else { 
      if ($fields['accreditations4']==6){echo 'HTML CODE 6';} else { 
      if ($fields['accreditations4']==7){echo 'HTML CODE 7';} else { 
      if ($fields['accreditations4']==0){echo '';}

      if ($fields['accreditations5']==1){echo 'HTML CODE 1';} else { 
      if ($fields['accreditations5']==2){echo 'HTML CODE 2';} else { 
      if ($fields['accreditations5']==3){echo 'HTML CODE 3';} else { 
      if ($fields['accreditations5']==4){echo 'HTML CODE 4';} else { 
      if ($fields['accreditations5']==5){echo 'HTML CODE 5';} else { 
      if ($fields['accreditations5']==6){echo 'HTML CODE 6';} else { 
      if ($fields['accreditations5']==7){echo 'HTML CODE 7';} else { 
      if ($fields['accreditations5']==0){echo '';}

      if ($fields['accreditations6']==1){echo 'HTML CODE 1';} else { 
      if ($fields['accreditations6']==2){echo 'HTML CODE 2';} else { 
      if ($fields['accreditations6']==3){echo 'HTML CODE 3';} else { 
      if ($fields['accreditations6']==4){echo 'HTML CODE 4';} else { 
      if ($fields['accreditations6']==5){echo 'HTML CODE 5';} else { 
      if ($fields['accreditations6']==6){echo 'HTML CODE 6';} else { 
      if ($fields['accreditations6']==7){echo 'HTML CODE 7';} else { 
      if ($fields['accreditations6']==0){echo '';}

      ?> 

首先我需要做些什么来关闭ifs吗?还有更短的方法吗?

4

3 回答 3

2

使用switch语句,像这样

switch( $fields['accreditations1'] ) {
  case 1: echo 'HTML CODE 1'; break;
  case 2: echo 'HTML CODE 2'; break;
  case 3: echo 'HTML CODE 3'; break;
  case 4: echo 'HTML CODE 4'; break;
  case 5: echo 'HTML CODE 5'; break;
  case 6: echo 'HTML CODE 6'; break;
  case 7: echo 'HTML CODE 7'; break;
  case 0: echo '';
}

我认为,这不是实际的输出。如果我弄错了,您应该考虑为此编写一个单独的函数!

于 2012-11-05T15:15:17.720 回答
1

试试这个,使用for循环,

 for( $i=1; $i<=count($fields); $i++) {
   if( $fields['accreditations'.$i] > 0 && $fields['accreditations'.$i] <= 7)
     echo 'HTML CODE ' $fields['accreditations'.$i];  
 }
于 2012-11-05T15:17:18.650 回答
1

我认为你得到了'太多之后<p>Accreditations</p>

除此之外,您还可以使用 switch 语句来稍微缩短这段代码。

您还可以使用:

if (condition) {}
else if (othercondition) {}
elseif (thirdcondition {}
else {}

请注意,else 的 if不在else的括号内。你可以写else ifor elseif,两者意思相同。

这在开关不起作用的情况下特别有用。在您的情况下,只需使用开关。

于 2012-11-05T15:18:36.593 回答