我的代码从数据库中选择,写出信息,然后运行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吗?还有一种更短的方法吗?
答案 0 :(得分:2)
使用switch
statement,就像这样
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 '';
}
我假设,这不是实际输出。如果我弄错了,你应该考虑为此写一个单独的函数!
答案 1 :(得分:1)
尝试使用for
循环
for( $i=1; $i<=count($fields); $i++) {
if( $fields['accreditations'.$i] > 0 && $fields['accreditations'.$i] <= 7)
echo 'HTML CODE ' $fields['accreditations'.$i];
}
答案 2 :(得分:1)
我认为在'
之后你有<p>Accreditations</p>
太多了。
除此之外,您可以使用switch语句来缩短此代码。
您也可以使用:
if (condition) {}
else if (othercondition) {}
elseif (thirdcondition {}
else {}
请注意,else的if是不在的else括号内。您可以写else if
或elseif
,两者的含义相同。
这在开关不起作用的情况下特别有用。在您的情况下,只需使用一个开关。