怎么打印 table before conditional switch php in the loop

问题描述 投票:0回答:3

我有表和表有我的字段的调用值的循环...

问题是每次我用<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>

更新 :

问题是每次值都是空的,在单元格中输入了错误的值!

Picture

php loops html-table
3个回答
4
投票

您可以在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;

}

这样你可以得到一个空值(以及任何其他值)。


1
投票

使用'默认'案例:

 <?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工作。


0
投票

如果我理解正确,

 <?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;
    }

} ?>
© www.soinside.com 2019 - 2024. All rights reserved.