是否有可能在列中填写一个表格中的值? [英] Is it possible to fill a table with values in column wise?

查看:71
本文介绍了是否有可能在列中填写一个表格中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能用列明智地填充表中的值。例如

Is it possible to fill table with values in column wise. For example

<?php
echo "<table>";
for ($j=0;$j<6;$j++)
{
echo "<tr>";
for ($i=0;$i<6;$i++)
{
echo "<td>".$j.$i."</td>";
}
echo "</tr>";
}
echo "</table>";
?>

输出成为

The output becomes

00  01  02  03  04  05
10  11  12  13  14  15
20  21  22  23  24  25
30  31  32  33  34  35
40  41  42  43  44  45
50  51  52  53  54  55

但我希望桌子看起来像

00  10  20  30  40  50
01  11  21  31  41  51
02  12  22  32  42  52
03  13  23  33  43  53
04  14  24  34  44  54
05  15  25  35  45  55

我通过不改变填入表中的值来满足条件。 (改变从$ j。$ i到$ i的回声。$ j带来了这种外观,但是我想以列的方式填充数据)。它如何成为可能?

I came with up with a condition by not changing values that fill in the table. (Altering echo from $j.$i to $i.$j brings that appearance but i wanted to fill data in column wise). How it become possible?

推荐答案

下面是你真正做到这一点,老派风格,完全评论解释。 >

Here's how you really do it, old-school style, fully commented for explanation.

 <?php
 //NOTE: This excercise would be slightly easier if you just used php's DomDocument class because you wouldn't 
 //need to waste some logic determining whether to open/close the table rows, but here it is just echoing
 //out some straight html 

//The example data
$data_set = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
//at the very least, you'll need to know how many columns you want to format the data into...
$number_of_columns = 5; 
//count the data items
$number_of_data_items=count($data_set);
//determine how many rows you'll need to display the data in the given number of columns
$num_rows=ceil($number_of_data_items/$number_of_columns);
//determine exactly how many cells it will take to display the data
$num_cells=$num_rows*$number_of_columns;

//now that we have enough info to output the table...
//init some counters
$row_count=0;
$column_count=0;
//open the table element
echo "<table border='1' cellspacing='0' cellpadding='5'>";
for($i=0;$i<$num_cells;$i++){

    $column_count++; 
    $index = $row_count + ($num_rows * ($column_count-1) );
    if( $column_count==1 ){ 
        echo "<tr>";
    } 

    if(  $index < $number_of_data_items){ 
        echo  "<td>Item :".($index+1)."</td>"; //Display the number of the data item we are
        echo  "<td>".$data_set[$index]."</td>"; //Display the actual data item
        echo "<td>&nbsp;&nbsp;</td>"; //Add some extra space between columns
    }else{ 
        //write empty cells if the data set doesn't completely fill the last column
        echo "<td>&nbsp;</td>";
        echo "<td>&nbsp;</td>";    
        echo "<td>&nbsp;&nbsp;</td>";  
    } 
    if($number_of_columns == $column_count){ 
        echo "</tr>";
        $row_count++; 
        $column_count=0;
    } 
} 
//close the table element
echo "</table>";
?>

这篇关于是否有可能在列中填写一个表格中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆