douzhuo5671
2014-08-14 14:28 阅读 13
已采纳

将数组值插入HTML表

I am trying to insert my array values into html table.

My Table need to have this limits: 5 columns, and the necessary rows, to fit all information.

Something like this:

1 | 2 | 3 | 4 | 5

11 | 22 | 33 | 44 | 55

111 | 222 | 333 | 444 | 555

Any ideas?


<table>
        <?php
            $i=0;
            $queryPiloto = queryPilot();
            foreach ( $queryPiloto AS $resultadoQueryPiloto ) { ?>

                <?php if ( $i < 5 ) { ?>
                    <td><input type="checkbox" value="<?php echo $resultadoQueryPiloto['Pilot']; ?>"><?php echo $resultadoQueryPiloto['Pilot'];?></td>
                <?php } 
                else { ?>
                <tr>
                    <td><input type="checkbox" value="<?php echo $resultadoQueryPiloto['Pilot']; ?>"><?php echo $resultadoQueryPiloto['Pilot'];?></td>
                </tr>
                <?php } ?>

            <?php $i++; } ?>
</table>

The data is shown like this:

REFER Piloto4 Piloto2 Piloto1 O99

O96

O95

O94

O90

O86

O80

O79

O77

O76


My Solution:

<?php

 $array= array(
    1,2,3,4,5,6,7,8,9,0,11,12,13,14,15
 );
 $verification = 5;

 echo '<table border="1">';
 echo '<tr>';
for($i = 0; $i < count($array); $i++ )
{
    if ( $i == $verification )
        echo '<tr>';

    if ( $i == $verification ) 
        $verification += 5;

    echo '<td>'.$array[$i].'</td>';

    if ( $i == $verification )
        echo '</tr>';
}
echo '<tr>';
echo '</table>';
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    duanhongqiong9460 duanhongqiong9460 2014-08-14 14:35

    you can put the limits:

    $array= array(
        array(1,2,3,4,5),
        array(11,22,33,44,55),
        array(111,222,333,444,555)
    );
    echo '<table border="1">';
    for($i = 0; $i < count($array); $i++ ){
        echo '<tr>';
        for($j = 0; $j < count($array[$i]); $j++ ){
            echo '<td>'.$array[$i][$j].'</td>';
        }
        echo '</tr>';
    }
    echo '</table>';
    

    your second question something like this:

    $array1 = array(1,2,3,4,5,6,7,8,9,0,12,13,14,60,3,4,5,56,77,6,456);
    $array2 = array();
    $array3 = array();
    
    $count=0;
    for($n = 0; $n < count($array1); $n++){
    
        $array2[] = $array1[$n];
        if($count == 4){
            $count = -1;
            array_push($array3,$array2);
            $array2 = array();
        }
        $count++;
    }
    
    var_dump($array3);
    
    点赞 评论 复制链接分享

相关推荐