【问题标题】:Retrieving data from a file and generation dynamic table从文件中检索数据并生成动态表
【发布时间】:2014-03-04 23:44:23
【问题描述】:

我有一个文件 File3:

[File3] 
Time | Name | Name | ID1 | ID2 
15:50 | Alex | Web | * 38 | 5 
10:50 | Xyz | Xxx | * 55 | 65 
10:50 | Volume | Xxx | 55 | 65 
12:50 | Kate | Uh | 35 | 62 
15:50 | Maria | Zzz | 38 | 67 

文件生成,时间只能有一行,下一次可能是30

我不擅长用PHP编写,想写一个脚本来下载文件并加载到动态表PHP中 带有标有 [*] 的标志的行,整行应该是红色的。

结果:

====================================
| Time | Name |Surname| ID1  | ID2 |
====================================
|15:50 | Alex |  Web  | * 38 |  5  | - red color background
====================================
|10:50 | Xyz  |   Xxx | * 55 | 65  | - red color background
====================================
|10:50 | asd  |   Xxx | 55   | 69  |
====================================
|12:50 | Kate |   Uh  | 35   | 62  |
====================================
|15:50 | Maria|   Zzz | 38   | 67  |
====================================

我在 html 中的脚本 php:

<div class="Xyz" style="width:800px;height:50px;">
<?php
print "<h1 align=\"left\"> </br></h1>";
$file = fopen("File3.txt", "r");
while (false !== ($line = fgets($file))) {
$wynik=explode("+",$line);
$wynik=array_slice($wynik,0,10);
$grid=pc_grid_horizontal($wynik,10);
print $grid;
}
function pc_grid_horizontal($array, $size) {
    $table_width = 100;
    $width = intval($table_width / $size);
    $tr = '<tr align="center">';
    $td = "<td width=\"$width%%\">%s</td>";
    $grid = "<table border=1 cellspacing=0 width=\"$table_width%\">$tr";
    $i = 0;
    foreach ($array as $e) {
        $grid .= sprintf($td, $e);
        $i++;
        if (!($i % $size)) {
            $grid .= "</tr>$tr";
        }
    }

    while ($i % $size) {
        $grid .= sprintf($td, '&nbsp;');
        $i++;
    }
    $end_tr_len = strlen($tr) * -1;
    if (substr($grid, $end_tr_len) != $tr) {
        $grid .= '</tr>';
    } else {
        $grid = substr($grid, 0, $end_tr_len);
    }
    $grid .= '</table>';

    return $grid;
}
?>

请帮帮我。

【问题讨论】:

    标签: php file dynamic html-table fopen


    【解决方案1】:

    为什么不做一些更简单的事情呢?您实际上不需要使用古老的 HTML 属性来设置整个表格的样式。您可以使用外部样式表中的 css。

    例如:

    <?php
    $file = file('File3.txt');
    
    $table = array();
    foreach($file as $line=>$content) {
       $parts = explode('|', $content); // turn your single line into an array of fields
       $parts = array_map('trim',$parts); // trim off any spaces from all parts
       if($line == 0) {
          $class = 'header'; // class for the header, that is the first line of the table
       } elseif($parts[3][0] == '*') {
          $class = 'emphasis'; // class for a row that needs to be red
       } else {
          $class = ''; // no class usually
       }
       $table[] = '<tr class="'.$class.'"><td>' . implode('</td><td>', $parts) . '</td></tr>';
    }
    
    echo '<table id="yourTbl">', implode("\r\n", $table), '</table>';
    

    然后添加包含以下内容的 CSS:

    table#yourTbl {
       width: xx px; /* set to whatever width you want the table to be. If you wrap it in a <div> with a fixed with, you can change this one to 100% */
    }
    #yourTbl td {
       /* default style for all table data fields. Overwritten for specific fields in the next 2 definitions */
       text-align: center; /* make sure all values are centered inside the table */
    }
    /* your first row, with the table heading */
    #yourTbl tr.header td {
       font-weight: bold;
    }
    /* your row with emphasis, which contains a * */
    #yourTbl tr.emphasis td {
       color: red;
    }
    

    【讨论】:

    • 我会在explode() 调用中使用' | '(包括空格)。
    • 我不知道他的数据是如何精确格式化的。我也不知道是否有任何管道被转义,或者它们是否被允许在值内部。以防万一,我在'|'上爆炸然后再把空格剪掉。
    • 我做了一个测试,我得到一个错误:解析错误:语法错误,脚本中第 14 行第 14 行 index.php 中的意外 T_FOREACH:foreach($file as $line=>$content) {
    • $table = array() 替换为$table = array();(添加分号)。
    • 我在 linii 13 中发现缺少“;”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 2018-12-14
    • 2014-09-07
    相关资源
    最近更新 更多