php 如何在php中创建html表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4436739/
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 13:03:25  来源:igfitidea点击:

how to create html table in php

phphtmlhtml-table

提问by cjd143SD

I have the following snippet of code that basically uses explode to split out these values:

我有以下代码片段,基本上使用爆炸来拆分这些值:

<?php
$data=array();
$Inputfile = file("prod.txt");
foreach ($Inputfile as $line){
   $pieces = explode(";", $line);
   echo $pieces[0];
   echo $pieces[1];
   echo $pieces[2];
   echo $pieces[3];
//print_r($line);
}
?>

Data: (prod.txt)

数据:(prod.txt)

PREFIX=abc;PART=null;FILE=/myprojects/school/out/data/feed_abc_2010120810.gz2
PREFIX=efg;PART=sdsu;FILE=mail_efg.dat.2010120810.gz2

Can someone show me how to put this in an HTML table dynamically, like this?? Is there an easy way to do this? Thanks.

有人可以告诉我如何像这样动态地将它放入 HTML 表格中吗??是否有捷径可寻?谢谢。

PREFIX  PART   FILE
abc     null   /myprojects/school/out/data/feed_abc_2010120810.gz2
efg     sdsu   mail_efg.dat.2010120810.gz2

采纳答案by Kevin

This should be flexible enough and won't require any hard-coded pair names:

这应该足够灵活,不需要任何硬编码的对名称:

<?php

$lines = preg_split('~\s*[\r\n]+\s*~', file_get_contents('prod.txt'));

foreach($lines as $i => $line) {
    $pairs = explode(';', $line);
    foreach($pairs as $pair) {
        list($column, $value) = explode('=', $pair, 2);
        $columns[$column] = true;
        $rows[$i][$column] = $value;
    }
}

$columns = array_keys($columns);


echo '<table><thead><tr>';

foreach($columns as $column) {
    echo '<th>'.$column.'</th>';
}

echo '</tr></thead><tbody>';

foreach($rows as $row) {
    echo '<tr>';
    foreach($columns as $column) {
        echo '<td>'.$row[$column].'</td>';
    }
    echo '</tr>';
}
echo '</tbody></table>';

?>

回答by systemovich

The Clean Way

清洁方式

<?php
$inputfile = file("prod.txt");

$data_lines = array();
foreach ($inputfile as $line)
{
    $data_lines[] = explode(";", $line);
}

//Get column headers.
$first_line = array();
foreach ($data_lines[0] as $dl)
{
    $first_line[] = explode("=", $dl);
}
$headers = array();
foreach ($first_line as $fl)
{
    $headers[] = $fl[0];
}

// Get row content.
$data_cells = array();
for ($i = 0; $i < count($data_lines); $i++)
{
    $data_cell = array();
    for ($j = 0; $j < count($headers); $j++)
    {
        $data_cell[$j] = substr($data_lines[$i][$j], strpos($data_lines[$i][$j], "=")+1);
    }
    $data_cells[$i] = $data_cell;
    unset($data_cell);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>HTML Table With PHP</title>
    </head>
    <body>
        <table border="1">
            <tr>
            <?php foreach ($headers as $header): ?>
                <th><?php echo $header; ?></th>
            <?php endforeach; ?>
            </tr>
        <?php foreach ($data_cells as $data_cell): ?>
            <tr>
            <?php for ($k = 0; $k < count($headers); $k++): ?>
                <td><?php echo $data_cell[$k]; ?></td>
            <?php endfor; ?>
            </tr>
        <?php endforeach; ?>
        </table>
    </body>
</html>

回答by Weston C

You've really got two problems here:

你真的有两个问题:

1) Parsing the information in each line into a record / associative array

1)将每一行的信息解析成记录/关联数组

2) Representing the series of these records/arrays as an HTML table.

2) 将这些记录/数组的系列表示为 HTML 表格。

Good code will separate these concerns somewhat.

好的代码会在某种程度上将这些问题分开。

function line2record($line) {
   $recordAA = array();
   $keyValuePairs = explode(';',$line);
   foreach($keyValuePairs as $kvp) {
       $pieces = explode('=',$kvp);
       $recordAA[$pieces[0]] = $pieces[1];
   }
   return $recordAA;
}

function record2TR($recordAA) {
   $str = implode('</td><td>',$recordAA);
   return "<tr><td>$str</td></tr>";
}

After that, it's pretty much a matter of applying these two functions to each line of the file:

之后,几乎就是将这两个函数应用于文件的每一行:

$Inputfile = file("prod.txt");
foreach ($Inputfile as $line)
    echo record2TR(line2record($line));

To get the header row, and the table/open close markup, you might do something like this:

要获取标题行和表格/打开关闭标记,您可以执行以下操作:

function record2TH($recordAA) {
    $headers = array_keys($recordAA);
    $str = implode('</th><th>',$headers);
    return "<tr><th>$str</th></tr>";
}

and then:

进而:

$fp = fopen('prod.txt','r');
$line = fgets($fp,MAX_LINE_LENGTH);  // you'd set this constant
$firstRecord = line2record($line);
echo "<table>";
echo record2TH($firstRecord);
echo record2TR($firstRecord);
while($line = fgets($fp,MAX_LINE_LENGTH))
    echo record2TR(line2record($line));
echo "</table>";

回答by Shakti Singh

echo '<table><tr><td>PREFIX</td><td>Part</td>File</td></tr>';
foreach ($Inputfile as $line){
   $pieces = explode(";", $line);
   $count=count($pieces);

   echo '<tr>';
   for ($counter=0; $counter <$count; $counter++)
   {
     echo '<td>'.$pieces[$counter].'<td>';
   } 
  echo '</tr>';
}

回答by David Kurid?a

Not the prettiest solution, just an example:

不是最漂亮的解决方案,只是一个例子:

echo '<table><tr><td>PREFIX</td><td>PART</td><td>FILE</td></tr>';
foreach ($Inputfile as $line)
{
    $pieces = explode(';', $line);
    echo sprintf(
        '<tr><td>%s</td><td>%s</td><td>%s</td></tr>',
        ltrim($pieces[0], 'PREFIX='),
        ltrim($pieces[1], 'PART='),
        ltrim($pieces[2], 'FILE=')
    );
}
echo '</table>';