drlq92444 2013-05-07 00:19
浏览 13
已采纳

PHP表:不能被3整除的数组不显示最后一行

I'm trying to split my array into 3 columns and the remaining that is not divisible by 3 to show as well. Currently it's not displaying. My code as below:

Count value is 8.

for($i=0; $i < count($subscriber); $i++) {

    if($i%3==0) $middle_data .= '<tr>';

    <td>Array fields goes here</td>

    if(($i+1)%3==0) $middle_data .= '</tr>';
}

Currently it shows 2 row but the last row that is not divisible by 3 is not appearing.

  • 写回答

2条回答 默认 最新

  • douxin0251 2013-05-07 00:57
    关注

    Your code echoing all values, but produce invalid markup. It looks like

    <table>
    <tr><td></td><td></td><td></td></tr>
    <tr><td></td><td></td><td></td></tr>
    <tr><td></td><td></td>
    </table>
    

    Try this one

    $subscriber = array_fill(0, 8, 'value');
    $count_column = 3;
    
    // loop by cells count, not values count
    $count_cell = ceil(count($subscriber) / $count_column) * $count_column;
    
    $middle_data = "<table>
    ";
    for ($i = 0; $i < $count_cell; $i++)
        {
        if ($i % $count_column == 0)
            $middle_data .= '<tr>';
    
        if (isset($subscriber[$i]))
            $middle_data .= '<td>' . $subscriber[$i] . '</td>';
        else
            $middle_data .= '<td></td>';
    
        if (($i + 1) % $count_column == 0)
            $middle_data .= "</tr>
    ";
        }
    $middle_data .= "</table>
    ";
    echo $middle_data;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗