简体   繁体   English

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

[英]PHP table: Array not divisible by 3 not showing last row

I'm trying to split my array into 3 columns and the remaining that is not divisible by 3 to show as well. 我试图将我的数组分成3列,其余的不能被3整除的列也显示出来。 Currently it's not displaying. 目前没有显示。 My code as below: 我的代码如下:

Count value is 8. 计数值为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行,但没有出现不能被3整除的最后一行。

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>\n";
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>\n";
    }
$middle_data .= "</table>\n";
echo $middle_data;

Try this one 试试这个

   <?php
    $subscriber= array('1','2','3','4','5',6,7,8);
    $middle_data="";

    if(count($subscriber))
    $middle_data .= '<tr>';

    for($i=0; $i < count($subscriber); $i++) 
    {        
        if((($i%3)==0 ) && $i!=0) 
        $middle_data .= '</tr><tr>';
        $middle_data .= " <td>$subscriber[$i] goes here </td>";
    }

    if(count($subscriber))
    $middle_data .= '</tr>';

    echo  $middle_data;
    ?>

output 输出

<tbody><tr> <td></td> <td></td> <td></td></tr> .... </tbody>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM