php 使用PHP动态生成表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22346707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:51:07  来源:igfitidea点击:

Dynamically generate table using PHP

phpformattinghtml-table

提问by Rocket

I know this has been asked before and I have got it working using the following code:

我知道以前有人问过这个问题,我已经使用以下代码让它工作了:

<?php
$maxcols = 8;  $i = 0;
echo "<table id='table1'><tr>";

foreach ($id as $k => $v) {
    echo "<td id='0'><div id='{$k}' class='drag t1'>{$v}</div></td>"; $i++;
    if ($i == $maxcols) { $i = 0; echo "</tr><tr>"; }
} $i++;


while ($i <= $maxcols) {
    $i++; echo "<td></td>";
}

echo "</tr></table>";
?>

This results in a table that looks like this :

这会生成一个如下所示的表:

enter image description here

在此处输入图片说明

I'd like to add headers to this so the end result looks like this:

我想为此添加标题,因此最终结果如下所示:

enter image description here

在此处输入图片说明

I'd like to do it dynamically so if I create a table that is only 5 columns wide I'd get on the first header row ID01 - ID05 and on the second header row ID06 - ID10

我想动态地做,所以如果我创建一个只有 5 列宽的表,我会在第一个标题行 ID01 - ID05 和第二个标题行 ID06 - ID10

I want to limit the header ID values to be no more than $maxidany extra header fields should be blank, like this : If $maxid = 12;then :

我想将标头 ID 值限制为不超过$maxid,任何额外的标头字段都应为空,如下所示: If $maxid = 12; 然后 :

enter image description here

在此处输入图片说明

I need the header rows are made as follows and not using <TH>

我需要按如下方式制作标题行,而不是使用 <TH>

<td class="mark">

I'm using some javascript to allow the movement of cell data.

我正在使用一些 javascript 来允许移动单元格数据。

The class is used to set the formatting on the header and stop items from being dragged into the fields.

该类用于设置标题的格式并阻止项目被拖到字段中。

Can anyone point me in the right direction on how to do this.

任何人都可以指出我如何做到这一点的正确方向。

回答by keepwalking

This should help you.

这应该对你有帮助。

$maxcols = 8; 
$maxid = 12;
$startid = 1;

echo "<table id='table1'>\n";
for ($i = 1;$i<=ceil($maxid/$maxcols);$i++) {

    echo "<tr>\n";
    for ($j=1;$j<=$maxcols;$j++)
        if ($startid <= $maxid)
            echo "  <td class='mark'>ID".$startid++."</td>\n";
        else 
            echo "  <td> </td>\n";

    echo "</tr>\n<tr>\n";
    for ($j=1;$j<=$maxcols;$j++)
        echo "<td>Content</td>\n";

    echo "</tr>\n";
}

echo "</table>\n";

Generates

生成

<table id='table1'>
    <tr>
        <td class='mark'>ID1</td>
        <td class='mark'>ID2</td>
        <td class='mark'>ID3</td>
        <td class='mark'>ID4</td>
        <td class='mark'>ID5</td>
        <td class='mark'>ID6</td>
        <td class='mark'>ID7</td>
        <td class='mark'>ID8</td>
    </tr>
    <tr>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
    </tr>
    <tr>
        <td class='mark'>ID9</td>
        <td class='mark'>ID10</td>
        <td class='mark'>ID11</td>
        <td class='mark'>ID12</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
        <td>Content</td>
    </tr>
</table>

回答by krishna

try this. It will work in the same way as you desired

尝试这个。它将以您想要的方式工作

<?php

$id= array("1","2","3","4","5","6","7","8","9","10","11","12");

$maxcols = 8;  $i = 0;$j=0;$t=0;$s=0;

$maxid = count($id);

echo "<table id='table1'><tr>";

foreach ($id as $k => $v) 
{

    if($t == 0)
    {

        while ($t <= $maxcols-1) {
                if($s < $maxid)
                {
                         $s++;$t++; echo "<td class='mark'>id$s</td>";
                }
                else
                {
                    echo "<td class='mark'></td>";$t++;$s++;
                }
        }
        echo "</tr><tr>";
    }
    else
    {

    }
        echo "<td id='0'><div id='{$k}' class='drag t1'>{$v}</div></td>"; $i++;
        if ($i == $maxcols) 
    { 
        echo "</tr><tr>"; 

        if($j == 0)
        {
            while ($j <= $maxcols-1) {
                if($s < $maxid)
                {
                     $s++;$j++; echo "<td class='mark'>id$s</td>";
                }
                else
                {
                    echo "<td class='mark'></td>";$j++;$s++;
                }
            }
            echo "</tr><tr>";

        }


        $i=0;

    }
} 

echo "</tr></table>";
?>

Output

输出

回答by Ferhat KO?ER

Hi You can Use My Library:

嗨,您可以使用我的图书馆:

class generate{
private $row = "<tr>{columns}</tr>";
private $td = "<td {attr}>{data}</td>";

private $attributeTR="";
private $attributeTD="";

private $tdBuilder="";

public function addCol($ColumValsArr=array("class='motota'"=>"Example")){
    foreach($ColumValsArr as $key=>$val){
        $newCol = str_replace("{data}",$val,$this->td); 
        $newCol = str_replace("{attr}",$key,$newCol);

        $this->tdBuilder .= str_replace("{data}",$key,$newCol); 
    }
}
public function getRow(){
    return str_replace("{columns}",$this->tdBuilder,$this->row);
}
}