转换html表格 [英] Transform html table
问题描述
我有一个按字母顺序排列的电影名称数组,我想创建一个 html 表,我想即时执行此操作,因此我执行了以下操作:
echo "";$i=0;foreach ($results as $entry){//如果是第4行,则开行如果($ i == 0){echo "\n";}//打印一个单元格echo "\t".$条目."</td>\n";我++;//如果第 4 行中的最后一个单元格,则关闭该行如果($ i == 4){echo "</tr>\n";$i=0;}}如果($i <4){while($i <4) {echo "\t \n";$i++;}echo "</tr>\n";}echo "</table></div>";然而,这会构建一个如下所示的表格:
entry0 |条目1 |条目2 |条目 3条目4 |条目5 |条目6 |条目7
我怎样才能建立一个像:
entry0 |条目3 |条目6条目1 |条目4 |条目7条目2 |条目5 |条目8
?
我猜我必须重新组织我的 $results 数组并仍然以相同的方式构建表?
我对 php 很陌生(一个星期!)所以我不确定如何去做
感谢您的帮助
解决方案 $results = array( 'e1', 'e2', 'e3', 'e4', 'e5', 'e6','e7');$NUM_COLUMNS = 3;$numRows = count($results)/$NUM_COLUMNS;如果(计数($结果)% $NUM_COLUMNS > 0){$numRows += 1;}echo "<div align=\"center\"><table>";$i=0;for ($i = 0; $i <$numRows; $i++) {echo "\n";$index = $i;for ($j = 0; $j <$NUM_COLUMNS; $j++) {//打印一个单元格$entry = '';如果 ($index < count($results)) {$entry = $results[$index];}echo "\t".$条目."</td>\n";$index += $numRows;}echo "</tr>\n";}echo "</table></div>";这是经过测试的,包括垂直排序项目.我想写一个描述,但我刚接到一个电话,不得不走了.如果您有任何问题,我会在约 1 小时内回答.(对不起!)
Hi I have an array of movie names in alphabetical order, of which I want to create a html table of, I want to do this on the fly so I did the following:
echo "<div align=\"center\"><table>";
$i=0;
foreach ($results as $entry){
//If first in row of 4, open row
if($i == 0) {
echo "<tr>\n";
}
//print a cell
echo "\t<td>" . $entry . "</td>\n";
i++;
//if last cell in row of 4, close row
if($i == 4) {
echo "</tr>\n";
$i=0;
}
}
if($i < 4) {
while($i < 4) {
echo "\t<td></td>\n";
$i++;
}
echo "</tr>\n";
}
echo "</table></div>";
However this builds a table which looks like:
entry0 | entry1 | entry2 | entry3
entry4 | entry5 | entry6 | entry7
How can I go about building a table like:
entry0 | entry3 | entry6
entry1 | entry4 | entry7
entry2 | entry5 | entry8
?
I'm guessing I would have to reorganise my $results array and still build the table the same way?
I'm very new to php ( a week!) so I'm not really sure how to go about this
Thanks for your help
解决方案 $results = array( 'e1', 'e2', 'e3', 'e4', 'e5', 'e6','e7' );
$NUM_COLUMNS = 3;
$numRows = count($results) / $NUM_COLUMNS;
if (count($results) % $NUM_COLUMNS > 0) {
$numRows += 1;
}
echo "<div align=\"center\"><table>";
$i=0;
for ($i = 0; $i < $numRows; $i++) {
echo "<tr>\n";
$index = $i;
for ($j = 0; $j < $NUM_COLUMNS; $j++) {
//print a cell
$entry = '';
if ($index < count($results)) {
$entry = $results[$index];
}
echo "\t<td>" . $entry . "</td>\n";
$index += $numRows;
}
echo "</tr>\n";
}
echo "</table></div>";
This is tested and includes sorting items vertically. I would write a description, but I just got a phone call and have to go. I will answer questions if you have any in ~1hr. (sorry!)
这篇关于转换html表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆