我有表和表有我的字段的调用值的循环...
问题是每次我用<td>
设置条件php switch
和每次值为空<td>
是隐藏和我的表变形和反汇编我怎么能打印<td></td>
无论如何打印值只是每次值不空?
<tr>
<?php foreach($rows as $array_value) {
switch ($array_value['value']) {
case "1": print '<td>one</td>'; break;
case "2": print '<td>two</td>'; break;
case "3": print '<td>three</td>'; break;
case "4": print '<td>four</td>'; break;
case "5": print '<td>five</td>'; break;
case "6": print '<td>six</td>'; break;
}
} ?>
</tr>
更新 :
问题是每次值都是空的,在单元格中输入了错误的值!
您可以在switch语句中添加default
以捕获除已定义的情况之外的任何其他情况:
switch ($array_value['value']) {
case "1": print '<td>one</td>'; break;
case "2": print '<td>two</td>'; break;
case "3": print '<td>three</td>'; break;
case "4": print '<td>four</td>'; break;
case "5": print '<td>five</td>'; break;
case "6": print '<td>six</td>'; break;
default: print '<td>empty</td>'; break;
}
这样你可以得到一个空值(以及任何其他值)。
使用'默认'案例:
<?php foreach($rows as $array_value) {
switch ($array_value['value']) {
case "1": print '<td>one</td>'; break;
case "2": print '<td>two</td>'; break;
case "3": print '<td>three</td>'; break;
case "4": print '<td>four</td>'; break;
case "5": print '<td>five</td>'; break;
case "6": print '<td>six</td>'; break;
default: print '<td></td>';// Add this line
}
} ?>
</tr>
当你省略default
值并且switch
获得的值与任何情况都不匹配时,td
中没有tr
并且表变形。当没有案例陈述成立时,default
工作。
如果我理解正确,
<?php foreach($rows as $array_value) {
switch ($array_value['value']) {
case "1": print '<td>one</td>'; break;
case "2": print '<td>two</td>'; break;
case "3": print '<td>three</td>'; break;
case "4": print '<td>four</td>'; break;
case "5": print '<td>five</td>'; break;
case "6": print '<td>six</td>'; break;
default: print '<td></td>'; break;
}
} ?>